Search code examples
google-apps-scriptgmailgoogle-sheetsgoogle-sheets-api

(Gmail) Sending emails from spreadsheet. How to add signature with image?


Due to the large amount of emails i'm sending with GMAIL i decided to automatize this process using a script and following this tutorial.Tutorial: Sending emails from a Spreadsheet

The "Message" is being generated by another function i created, called prepareEmails.

The problems are the following:

1) How can i tell prepareEmails to add my personal signature? I can't simply copy its text into that function, because my signature contains an image (for which i have the URL), and i want that image to be into the signature.

2) How can i make my signature BOLD?

Thanks everybody


Solution

  • There is an open Issue 2441 requesting the ability to append gmail signatures to email when using the GMailService. Visit and star it to receive updates.

    As @wchiquito suggests, you can craft a script to attach images, producing a signature. You can also use HTML tags such as <B></B> to render text in bold, and so on.

    Here's a different approach that will instead use a draft email as a template. This way, you can produce your signature with a variety of fonts and images using the online editor, and end up with a capability similar to automatic signature insertion.

    The template needs to be saved in your Drafts folder, and it needs to have a tag indicating where the body of emails should go.

    Screenshot

    Example

    function sendWithTemplate() {
      var msgBody = "Test of sending a message using a template with a signature.";
      sendGmailTemplate(Session.getActiveUser().getEmail(), 'test', msgBody );
    }
    

    Screenshot

    Script

    /**
     * Insert the given email body text into an email template, and send
     * it to the indicated recipient. The template is a draft message with
     * the subject "TEMPLATE"; if the template message is not found, an
     * exception will be thrown. The template must contain text indicating
     * where email content should be placed: {BODY}.
     *
     * @param {String} recipient  Email address to send message to.
     * @param {String} subject    Subject line for email.
     * @param {String} body       Email content, may be plain text or HTML.
     * @param {Object} options    (optional) Options as supported by GmailApp.
     *
     * @returns        GmailApp   the Gmail service, useful for chaining
     */
    function sendGmailTemplate(recipient, subject, body, options) {
      options = options || {};  // default is no options
      var drafts = GmailApp.getDraftMessages();
      var found = false;
      for (var i=0; i<drafts.length && !found; i++) {
        if (drafts[i].getSubject() == "TEMPLATE") {
          found = true;
          var template = drafts[i];
        }
      }
      if (!found) throw new Error( "TEMPLATE not found in drafts folder" );
    
      // Generate htmlBody from template, with provided text body
      var imgUpdates = updateInlineImages(template);
      options.htmlBody = imgUpdates.templateBody.replace('{BODY}', body);
      options.attachments = imgUpdates.attachments;
      options.inlineImages = imgUpdates.inlineImages;
      return GmailApp.sendEmail(recipient, subject, body, options);
    }
    
    
    /**
     * This function was adapted from YetAnotherMailMerge by Romain Vaillard.
     * Given a template email message, identify any attachments that are used
     * as inline images in the message, and move them from the attachments list
     * to the inlineImages list, updating the body of the message accordingly.
     *
     * @param   {GmailMessage} template  Message to use as template
     * @returns {Object}                 An object containing the updated 
     *                                   templateBody, attachments and inlineImages.
     */
    function updateInlineImages(template) {
      //////////////////////////////////////////////////////////////////////////////
      // Get inline images and make sure they stay as inline images
      //////////////////////////////////////////////////////////////////////////////
      var templateBody = template.getBody();
      var rawContent = template.getRawContent();
      var attachments = template.getAttachments();
    
      var regMessageId = new RegExp(template.getId(), "g");
      if (templateBody.match(regMessageId) != null) {
        var inlineImages = {};
        var nbrOfImg = templateBody.match(regMessageId).length;
        var imgVars = templateBody.match(/<img[^>]+>/g);
        var imgToReplace = [];
        if(imgVars != null){
          for (var i = 0; i < imgVars.length; i++) {
            if (imgVars[i].search(regMessageId) != -1) {
              var id = imgVars[i].match(/realattid=([^&]+)&/);
              if (id != null) {
                var temp = rawContent.split(id[1])[1];
                temp = temp.substr(temp.lastIndexOf('Content-Type'));
                var imgTitle = temp.match(/name="([^"]+)"/);
                if (imgTitle != null) imgToReplace.push([imgTitle[1], imgVars[i], id[1]]);
              }
            }
          }
        }
        for (var i = 0; i < imgToReplace.length; i++) {
          for (var j = 0; j < attachments.length; j++) {
            if(attachments[j].getName() == imgToReplace[i][0]) {
              inlineImages[imgToReplace[i][2]] = attachments[j].copyBlob();
              attachments.splice(j, 1);
              var newImg = imgToReplace[i][1].replace(/src="[^\"]+\"/, "src=\"cid:" + imgToReplace[i][2] + "\"");
              templateBody = templateBody.replace(imgToReplace[i][1], newImg);
            }
          }
        }
      }
      var updatedTemplate = {
        templateBody: templateBody,
        attachments: attachments,
        inlineImages: inlineImages
      }
      return updatedTemplate;
    }
    

    Credit where credit is due: The "Yet Another Mail Merge" script includes code that preserves inline images in emails during a mail merge - I've borrowed from that. Thanks Romain!