(Gmail)从电子表格发送电子邮件。如何添加图像签名?

时间:2013-08-28 16:50:16

标签: google-apps-script gmail google-sheets google-spreadsheet-api

由于我使用GMAIL发送大量电子邮件,我决定使用脚本自动化此过程并遵循本教程。Tutorial: Sending emails from a Spreadsheet

“消息”由我创建的另一个函数生成,名为 prepareEmails

问题如下:

1)我如何告诉 prepareEmails 添加我的个人签名?我不能简单地将其文本复制到该函数中,因为我的签名包含一个图像(我有URL),我希望该图像进入签名。

2)我怎样才能让我的签名大胆?

谢谢大家

2 个答案:

答案 0 :(得分:6)

有一个开放的Issue 2441请求能够在使用GMailService时将gmail签名附加到电子邮件中。访问并加星标以接收更新。

正如@wchiquito建议的那样,你可以制作一个脚本来附加图像,产生一个签名。您还可以使用<B></B>等HTML标记以粗体显示文本,依此类推。

这是一种不同的方法,它将使用草稿电子邮件作为模板。这样,您可以使用在线编辑器生成包含各种字体和图像的签名,并最终获得类似于自动签名插入的功能。

模板需要保存在“草稿”文件夹中,并且需要有一个标签,指明电子邮件正文的位置。

Screenshot

实施例

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

Screenshot

脚本

/**
 * 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;
}

信用到期的信用:“又一个邮件合并”脚本包括在邮件合并期间保留电子邮件中的内联图像的代码 - 我从中借用了这些代码。谢谢罗曼!

答案 1 :(得分:2)

目前,API不提供任何内容来包含邮件签名,但是,如果您可以控制签名,则可以使用GmailApp / MailApp类中提供的方法sendEmail(recipient, subject, body, options)options参数允许您设置其他参数以获得所需内容,例如,包含图像,签名可以手动构建,并以粗体显示文本。

我邀请您查看文档/示例并发布您可能遇到的任何问题。可以找到一个有趣的例子here