Search code examples
google-apps-scriptgoogle-sheetsgoogle-drive-apihtml-email

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

}

Solution

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