(Gmail) 从电子表格发送电子邮件.如何在图像中添加签名? [英] (Gmail) Sending emails from spreadsheet. How to add signature with image?

查看:10
本文介绍了(Gmail) 从电子表格发送电子邮件.如何在图像中添加签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我使用 GMAIL 发送大量电子邮件,我决定使用脚本并按照本教程自动执行此过程.教程:从电子表格发送电子邮件

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

消息"是由我创建的另一个函数生成的,称为 prepareEmails.

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

问题如下:

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

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) 我怎样才能使我的签名加粗?

2) How can i make my signature BOLD?

谢谢大家

推荐答案

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

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.

正如@wchiquito 所建议的,您可以编写一个脚本来附加图像,生成签名.您还可以使用 HTML 标签,例如 <B></B> 以粗体呈现文本,等等.

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.

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

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

信用到期的信用:Yet Another Mail Merge"脚本包含在邮件合并期间保留电子邮件中的内联图像的代码 - 我已经从中借用了.谢谢罗曼!

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!

这篇关于(Gmail) 从电子表格发送电子邮件.如何在图像中添加签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆