简体   繁体   English

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

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

Is there a way with Javascript (or other) from Google Spreadsheet to get the Gmail account signature? Google Spreadsheet中的Java脚本(或其他)是否可以获取Gmail帐户签名?

Details: I have a Google spreadsheet with information in it. 详细信息:我有一个包含信息的Google电子表格。 When clicking on a button, it identifies who has to receive the information, prepare a mail, and send it to the person. 单击按钮时,它标识谁必须接收信息,准备邮件并将其发送给该人。 However, I want to add the sender's signature at the end of the mail (it includes name, phone, logo, etc.). 但是,我想在邮件末尾添加发件人的签名(包括姓名,电话,徽标等)。 I'm open if I get the signature from an other place, as long as it can change according to who's sending the mail. 只要我可以从其他地方获得签名,我就会开放,只要它可以根据谁发送邮件而变化。 This is for my volunteering association, not a job. 这是给我的志愿协会的,不是工作。

In case my code may help (I look to fill the var Signature): 如果我的代码可能会有所帮助(我希望填写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);

SOLUTION: 解:

I've find a way from another site (can't find it): Create a template in your gmail draft with your signature, and put in the subject "Template" and in the body {Body}. 我从另一个网站找到了一种方法(找不到):在gmail草稿中创建带有您的签名的模板,然后将主题“模板”和正文{Body}放入其中。 Then use the following code to create a copy of the mail, and fill it with all the information: 然后,使用以下代码创建邮件的副本,并在其中填充所有信息:

In the function to send the mail add: 在发送邮件的功能中添加:

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

Referring to the following functions: 参考以下功能:

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

There is not a method to request a signature that you have already created. 没有一种方法可以请求您已经创建的签名。 It is possible to create one yourself with inlineImages. 可以使用inlineImages自己创建一个。 The documentation for that can be found here: https://developers.google.com/apps-script/reference/gmail/gmail-app#sendemailrecipient-subject-body-options 可以在以下位置找到该文档的文档: 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