Adding a hyperlink to an inline image on Google Appscript

430 views Asked by At

I have the below code which adds an inline image to an email that gets sent from the google sheet. I am working on adding a hyperlink to the inline image. Any guidance on how to achieve this would be appreciated!

    var pwcURL2 = DriveApp.getFileById("1C56M6DeZCm9IK5K26Z_ZYNLMb8rdqB4a").getBlob();

    var pwcblob = UrlFetchApp
                            .fetch(pwcURL)
                            .getBlob()
                            .setName(pwcblob)

//Some more lines of code in the middle which I have skipped as they are not relevant to the question being asked here

    bodypwc = "<img src='cid:pwc' style='width:24px; height:16px;'/>" + bodypwc;
    MailApp.sendEmail({ 
      to: currentEmail,
      subject: subjectLine, 
      body: messageBody,
      attachments: [liabilityWaiver1,liabilityWaiver2],
      htmlBody: messageBody+"<BR/><BR/>"+"<img src=\"cid:sampleImage\">",
      inlineImages: {sampleImage: pwcURL2}
    });

1

There are 1 answers

3
Tanaike On BEST ANSWER
  • You want to add the hyperlink to the inline image of Gmail.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

In this case, how about adding the hyperlink to <img src=\"cid:sampleImage\">?

Modified script:

When your script is modified, it becomes as follows.

function myFunction() {
  var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png";  // Sample image
  var image = UrlFetchApp.fetch(url).getBlob();
  var currentEmail = "###";  // Please set the email address.
  var subjectLine = "sample subject";  // Please set the subject.
  var messageBody = "sample message body";  // Please set the email body.
  var urlForInlineImage = "https://stackoverflow.com/q/60689970/7108653";  // Please set the URL you want to add to the inline image. Here, the URL of this question is used as a sample URL.

  MailApp.sendEmail({ 
    to: currentEmail,
    subject: subjectLine,
    body: messageBody,
  //    attachments: [liabilityWaiver1,liabilityWaiver2],  // Here, as a sample case, no attachment files are used.
    htmlBody: messageBody+"<BR/><BR/>"+"<a href=\"" + urlForInlineImage + "\"><img src=\"cid:sampleImage\"></a>",
    inlineImages: {sampleImage: image}
  });
}
  • When you run the script, you can see an email including the inline image with the hyperlink.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.