繁体   English   中英

Javascript使用签名在Gmail上发送邮件,使用Google Spreadsheet在BCC上发送邮件

[英]Javascript Send mail on Gmail with signature and BCC with Google Spreadsheet

Google Spreadsheet中的Java脚本(或其他)是否可以获取Gmail帐户签名?

详细信息:我有一个包含信息的Google电子表格。 单击按钮时,它标识谁必须接收信息,准备邮件并将其发送给该人。 但是,我想在邮件末尾添加发件人的签名(包括姓名,电话,徽标等)。 只要我可以从其他地方获得签名,我就会开放,只要它可以根据谁发送邮件而变化。 这是给我的志愿协会的,不是工作。

如果我的代码可能会有所帮助(我希望填写var签名):

 var Signature;
 var Receivers;
 var Subject;
 var Location ;
    var MailtoSend= "Hello, \n\n The next meeting will be at " + Location + ".  \n" + Signature;
    MailApp.sendEmail(Receivers, Subject, MailtoSend);

解:

我从另一个网站找到了一种方法(找不到):在gmail草稿中创建带有您的签名的模板,然后将主题“模板”和正文{Body}放入其中。 然后,使用以下代码创建邮件的副本,并在其中填充所有信息:

在发送邮件的功能中添加:

sendGmailTemplate(RecipientTo, RecipientCC, RecipientBCC, SujetAEnvoyer, CourrielAEnvoyer);

参考以下功能:

function sendGmailTemplate(RecipientTo, RecipientCC, RecipientBCC, subject, body, options) { //mettre à jour la quantité de recipeien cc bcc to
  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( "Impossible de trouver le brouillon 'Template' sur le gmail" );

  // 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;
  options.cc = RecipientCC;
  options.bcc = RecipientBCC;
  options.replyTo = "";

  return GmailApp.sendEmail(RecipientTo, subject, body, options); 
}


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

没有一种方法可以请求您已经创建的签名。 可以使用inlineImages自己创建一个。 可以在以下位置找到该文档的文档: https : //developers.google.com/apps-script/reference/gmail/gmail-app#sendemailrecipient-subject-body-options

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM