1

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}
    });

Mishal
  • 450
  • 9
  • 27

1 Answers1

3
  • 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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks @Tanaike for your help. I appreciate it. would you be able to help me with this [new question](https://stackoverflow.com/questions/61622332/how-to-optimise-apps-script-code-by-using-arrays-to-pull-data-from-google-sheets) – Mishal May 06 '20 at 04:50
  • 1
    @Mohammed Mishal Thank you for replying. When I saw it, I noticed that an answer has already been posted. I believe that the answerer is thinking the modification points for resolving your issue now. So how about waiting for it? I would like to respect the existing answer. – Tanaike May 06 '20 at 05:38
  • 1
    yes sounds good. I appreciate your ethics and would love to learn more from you. Thank you for your quick reply. – Mishal May 06 '20 at 06:23