Mous
Mous

Reputation: 159

Embed logo into email

I'm wondering how can I embed a logo into email. The logo is saved on Google Drive. Also, I know the reference inlineimage and blob need to be used but I don't know how.

I tried this code, but no success

var Img = DriveApp.getFileById(1pRBZ....cKMFll1OouC..er2V97e8...).getBlob();

The line below retreive the message template. So, I want to include the image into the message as a signature.

var TemplateTexte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();

function EnvoiIDCourriel() {

  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Liste").activate();
  var SS = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var LR = SS.getLastRow()

  var TemplateTexte = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
  //Permet d'utiliser le corps du courriel qui se trouve dans l'onglet Template


  //Logger.log(NomCandidat);

  //Cette ligne permet de passer à travers de toutes les lignes inscrites
  var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
  values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
    if (check === true) {
       var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
       var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage);
  }
});

}

Upvotes: 1

Views: 546

Answers (2)

Tanaike
Tanaike

Reputation: 201428

I believe your goal as follows.

  • You want to add a logo image to the email using the inlineimage.
  • The logo image file is put in your Google Drive.

For this, how about this answer?

Modification points:

  • In order to add the logo image to the email, it is required to use the HTML body.
  • In this case, options of sendEmail(recipient, subject, body, options) is used.

Modified script:

When your script is modified, it becomes as follows.

From:
  var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
  values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
    if (check === true) {
       var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
       var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage);
  }
});
To:
var fileId = "###";  // Added: Please set the file ID of the logo image.
var blob = DriveApp.getFileById(fileId).getBlob();  // Added
var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
  if (check === true) {
    var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
    var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    var html = CorpsMessage + '<BR><img src="cid:logo">';  // Added
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage, {htmlBody: html, inlineImages: {logo: blob}});  // Modified
  }
});

or

To:

As other modification, this is the method for using the HTML template. By this, you can prepare the HTML body of the email as a file.

var fileId = "###";  // Added: Please set the file ID of the logo image.
var blob = DriveApp.getFileById(fileId).getBlob();  // Added
var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
  if (check === true) {
    var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
    var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    var html = HtmlService.createTemplateFromFile("index");  // Added
    html.text = CorpsMessage;  // Added
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage, {htmlBody: html.evaluate().getContent(), inlineImages: {logo: blob}});  // Added
  }
});

And, please create a HTML file at the script editor as the filename of index.html and copy&paste the following HTML.

<!DOCTYPE html>
<html>
  <body>
    <?= text ?>
    <BR>
    <img src="cid:logo">
  </body>
</html>

Note:

  • In this modified script, CorpsMessage + '<BR><img src="cid:logo">' is used as the HTML body. So if you want to modify for your actual situation, please modify this.
    • logo of cid:logo is corresponding to logo of inlineImages: {logo: blob}.
  • If the email client cannot read the HTML email, the logo is not shown. Please be careful this.

References:

Added:

  • From your replying, you want to use the email template as follows.

    Bonjour {Nom},
    
    Ceci est vorte code d'identification : {ID}
    
    Ce code devra être utillisé lors de votre examen en Iigne pour le processus : {Processus}.
    
    Bon succés !
    
    ---
    Equipe des tests en ligne
    
    Service des Ressources Humaines
    

For this, how about the following modification? In this case, please modify above template as follows.

From:

Equipe des tests en ligne

To:

{LOGO}

In this modification, {LOGO} is replaced with the logo image.

Modified script:

Please modify your script as follows.

From:
  var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
  values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
    if (check === true) {
       var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
       var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage);
  }
});
To:
var fileId = "###";  // Added: Please set the file ID of the logo image.
var blob = DriveApp.getFileById(fileId).getBlob();  // Added
var values = SS.getRange("A2:E" + SS.getLastRow()).getValues();
values.forEach(([check, NomCandidat, ID, CurrentEmail, Processus]) => {
  if (check === true) {
    var CorpsMessage = TemplateTexte.replace("{Nom}",NomCandidat).replace("{ID}",ID).replace("{Processus}",Processus);
    var ObjetCourriel = "Code d'identification: " + ID + " - Test en ligne";
    var html = CorpsMessage.replace(/\n/g, '<br>').replace('{LOGO}', '<img src="cid:logo">');  // Added
    GmailApp.sendEmail(CurrentEmail, ObjetCourriel, CorpsMessage, {htmlBody: html, inlineImages: {logo: blob}});  // Modified
  }
});

Upvotes: 2

sharken
sharken

Reputation: 57

To send an image from your Google Drive in an email:
1. Make the image file as sharing to "Anyone with the link"
2. Copy the image ID from the shared link
3. In your application which is sending the email retrieve this image as a blob

var imageBlob = DriveApp
                  .getFileById(imageID)
                  .getBlob()
                  .setName("Logo"); 

To include the image in your email, you need to compose your email using html tags. Declare a variable to hold the html script.

var html = ""

The above variable will be used to hold the contents of your entire email, using html tags to preserve formatting.
If html tags are not included, the email may not be displayed correctly across devices. There may be mislaignment of paragraphs, tables, images may not be displayed, etc.

html = '<body>' + 
html = html + '<p style="color:grey; font-family: Palatino, Times, serif; font-size: 16px;font-style: italic;">';
html = html + CorpsMessage + '</p>';
html = html + '<div align=' + imageAlignment +'><img src="cid:image" width=' + imageWidth + '%height='+ imageHeight + '% align=' + imageAlignment +'></div><br><br></body>';

You then send your email using GmailApp.sendEmail or MailApp.sendEmail:

try{
  MailApp.sendEmail(emailAddress, subject, '',{ 
  cc: emailCc,
  bcc: emailBcc,
  htmlBody: html,
  inlineImages:{image: imageBlob}});
  sheet.getRange("G28").setFontColor("#228B22");
  sheet.getRange("G28").setValue("Latest update: Mail sent @" + new Date());  
    }catch(e){
      sheet.getRange("G28").setFontColor("#FF0000")
      sheet.getRange("G28").setValue("Alert! Error: " + "  " + new Date() + "  " + e.message)
  }

Always enclose sendEmail method in a try/catch block, if for any reason there is an error even in 1 message, all messages following the error message will not be sent. This is irrespective of the email being in simple text format or in html format.

Enclosing a try/catch block prevents sending only erroneous messages. Correct messages only will be sent.

Upvotes: 1

Related Questions