1

I tried to add a Hyperlink Image to the end of my email message. Attaching pdf and adding an image works well, but I have no clue how to do the same with a Hyperlink Image.

My previous sources were: Add Image to Google Sheets email App Script https://developers.google.com/apps-script/reference/mail/mail-app#sendemailmessage

Code:

function emailSend(){

// Auf das aktive Tabellenblatt zugreifen
var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Invoice")
var as = SpreadsheetApp.openById("1ffdhHQC4mpBTy8RzAcgXOrFv_fSXXXd3g1Ajx_EEcLguOvU").getSheetByName("Test") 
var is = SpreadsheetApp.openById("138nWL92xj1KTJdfhdh4iaOn_c62o5Qy-YynLFk1kAIky1TUF8").getSheetByName("Tabellenblatt1")
var ks = SpreadsheetApp.openById("afdfd5_s1HQC4mpBTy8RzAcgXOrFv_fSXXXd3g1Ajx_EEcLguOvU").getSheetByName("Kunden")

var getData = Browser.inputBox('Sende Rechnungsemail', 'Schreib: OK', Browser.Buttons.OK_CANCEL);
if (getData != 'cancel') 
{
  if (getData == 'OK')
  {
//Rechnungsverzeichnis
  var rechnungen = SpreadsheetApp.openById("1HQC4mpBTy8RzAcgXOrFv_fSXXXd3g1Ajx_EEcLguOvU").getSheetByName("Kundenrechnungen");
  var row = 2;
  var column = 2;
  while(rechnungen.getRange(row, column).getValue() != '')
  {
    row++;

  }
  //is.getRange(18,4).setValue(rechnungen.getRange(row,1).getDisplayValue());
  rechnungen.getRange(row,column).setValue(new Date);
  rechnungen.getRange(row,3).setValue(as.getRange("A3").getValue());
  rechnungen.getRange(row,4).setValue(as.getRange("B3").getValue());
  rechnungen.getRange(row,5).setValue(as.getRange("C3").getValue());
  rechnungen.getRange(row,6).setValue(as.getRange("D3").getValue());
  rechnungen.getRange(row,7).setValue(as.getRange("K3").getValue());
  rechnungen.getRange(row,8).setValue(as.getRange("L3").getValue());
  rechnungen.getRange(row,9).setValue(as.getRange("M3").getValue());


// Den Betreff für die Email aus Zelle B2 auslesen
var betreff = as.getRange("B5").getValue();

// Die Emailadresse für den Empfänger aus Zelle B3 auslesen
//var emailEmpfaengerAdresse = as.getRange("G3").getValues();
    var emailEmpfaengerAdresse = "xxx@googlemail.com";

// Die Nachricht für die Email aus der Zelle B4 auslesen
var nachricht = as.getRange("B6").getValues();

var nachricht = "Hi"+ "<p>" + '<img src="cid:image"><br/>' + "</p>";

//Bild    
var blob = UrlFetchApp.fetch("http://example.com/wp-content/uploads/2019/08/email-2.jpg").getBlob();


// Das Tabellenblatt als PDF der Variablen pdf zuweisen


var pdf = DriveApp.getFileById('55435hs1138nWL92xj1KTJiaOn_c62o5Qy-YynLFk1kAIky1TUF8').getAs('application/pdf').getBytes();

// Den Anhang festlegen (Name des Anhangs, Inhalt, Typ)
var anhang = {fileName:(betreff+".pdf"),content:pdf, mimeType:'application/pdf'};

//Nachfrage

// Senden der Email
var nachricht2 = nachricht.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); 
    MailApp.sendEmail(emailEmpfaengerAdresse, betreff, nachricht2,{ htmlBody: nachricht, inlineImages: {image:blob }, attachments:anhang});

//Sendung an Selbst
MailApp.sendEmail("xxx@xxx.com", (SpreadsheetApp.getActiveSpreadsheet().getRange("B3").getValue()+" "+ SpreadsheetApp.getActiveSpreadsheet().getRange("A3").getValue() + " Rechung"), nachricht, {attachments:[anhang]}); 
  }
}

}

Thank you!

04k
  • 53
  • 7
  • How do you want to put the image of hyperlink to the email? Can you provide the output you expect? – Tanaike Oct 10 '19 at 22:23
  • I would like to add these little facebook and twitter hyperlink images in my email signature - under the companylogo and adress. Thanks. – 04k Oct 11 '19 at 10:29
  • Thank you for replying. I proposed a sample script and modified script. Could you please confirm it? If I misunderstood your goal and that was not the result you want, I apologize. – Tanaike Oct 12 '19 at 00:07

1 Answers1

2
  • You want to put an image with the hyperlink to the email when the email is sent using MailApp.sendEmail().

If my understanding is correct, how about this answer? The image can be put with the hyperlink using HTML body. A simple sample script is as follows.

Sample script:

In this sample script, a logo is put as an image with the hyperlink.

var email = "###"; // Please set an email address.
var htmlBody = '<a href="https://stackoverflow.com/company/logos"><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png" alt="sample" width="30%" height="30%"></a>';
MailApp.sendEmail({to: email, subject: "sample subject", body: "sample body", htmlBody: htmlBody});
  • Also you can use GmailApp like GmailApp.sendEmail(email, "sample subject", "sample body", {htmlBody: htmlBody}).
  • In this case, it is required to be able to see the email with the HTML body.

Modified script:

When your script is modified and the image is put to the last of the HTML body, it became as follows.

Please add the following script before MailApp.sendEmail(emailEmpfaengerAdresse, betreff, nachricht2,{ htmlBody: nachricht, inlineImages: {image:blob }, attachments:anhang});.

nachricht += '<a href="https://stackoverflow.com/company/logos"><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png" alt="sample" width="30%" height="30%"></a>';

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165