Home > Mobile >  Create image link in html using Google Apps Script in email (html) body
Create image link in html using Google Apps Script in email (html) body

Time:03-16

I'm trying to dynamically generate a image link for a youtube video with the code above:

var emailMessage = HtmlService.createTemplateFromFile("bdaycard");

var msgMate = {};
msgMate.msgNiver = msgNiver;
// ...
msgMate.linkVideo = getURL_(getVideosArt["id"]); //some youtube id video
    
emailMessage.msgMate = msgMate;

var message = emailMessage.evaluate().getContent();

MailApp.sendEmail(emailAddress, subject, emailMessage, {
      htmlBody: message,
}); 


function getURL_(videoId) {
  
  var urlVideo = "https://www.youtube.com/embed/"   videoId;
  var thumVideo = "https://i.ytimg.com/vi/"   videoId   "/hqdefault.jpg";
  
  var urlA = '<a href="'   urlVideo   '" target="_blank" <img src="'   thumVideo   '" width="480" height="360"></a>';
  
  return urlA;
}

And my html page is configured with scriptlets as bellow

<div>
 <?= msgMate.linkVideo ?>
</div>

Unfortunately as a result I'm getting the text instead of an image link as desired

enter image description here

There is a way to show the image instead of the snippet code in the final html/body email result?

CodePudding user response:

In your script, how about the following modification?

From:

var urlA = '<a href="'   urlVideo   '" target="_blank" <img src="'   thumVideo   '" width="480" height="360"></a>';

To:

var urlA = '<a href="'   urlVideo   '" target="_blank"> <img src="'   thumVideo   '" width="480" height="360"></a>';

And also,

From:

<div>
 <?= msgMate.linkVideo ?>
</div>

To:

<div>
 <?!= msgMate.linkVideo ?>
</div>
  • In your HTML, at <a href="' urlVideo '" target="_blank" <img, the tag a is not enclosed.
  • In this case, please use <?!= instead of <?=.

Reference:

  • Related