使用Google脚本发送带有图片附件和正文的电子邮件 [英] Send email with picture attachment and body using google script

查看:134
本文介绍了使用Google脚本发送带有图片附件和正文的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我无法发送带有图片附件的电子邮件.因为图像文件名是随机的,因此无法确定邮件正文是否适合从驱动器发送的图像.这是我已完成的过程的逐步说明:

Hi im having trouble in sending email with image attachment. because the image file name is randomized and have no way to find out if the body of my message fits the image it will send from my drive. Here is a step by step of the process i have done:

  1. 将在线表单集成到Google电子表格(完成)
  2. 联机到Google驱动器的表单(完成)(电子表格每行中的图像均按文件夹保存,文件夹名称包含唯一的ID,该ID也出现在每一行的电子表格单元格中)
  3. 我想在这里做的是通过a.(搜索包含ceratin文本的文件夹名称)获取google drive中文件夹的图像. b.(获取文件夹内容.)(均为图像) c.(将文件夹的内容附加到电子邮件中.)
  1. online form integration to google spreadsheet (done)
  2. online form to google drive (done) (images from each row of spreadsheet are saved by folder with folder name contains a unique id that is also present in the spreadsheet cell of each row)
  3. What i would like to do here is get the images of folder in google drive by a.(searching the folder name which contains a ceratin text) b.(getting the folder contents.)(all are images) c.(attaching the contents of the folder to the email .)

示例:

function send() {
  var picture1 = DriveApp.getFilesByName('snottyboy.jpg');
  var picture2     = DriveApp.getFilesByName('daryl.jpg');
  var recipientsTO = "fgh@gmail.com" + "," + "sdd@gmail.com"+ "," + "spaz@gmail.com"+ "," + "def@gmail.com"+ "," + "abc@gmail.com";
  MailApp.sendEmail({
    to:recipientsTO, 
    subject: "LOOK A LIKE",   
    body:"Final Message",  
    attachments: [picture1.next(),picture2.next()]
  });
}

谢谢您的帮助.

查看图片:

推荐答案

要附加文件,请使用File.getBlob()将其作为blob附加.例如:

To attach a file, you use File.getBlob() to attach it as a blob. For example:

attachments: [picture1.next().getBlob(),picture2.next().getBlob()]

如果您知道文件的确切ID(例如'0BxDqyd_bUCmvN1E3N0dQOWgycEF'),则可以使用blob的形式获取它,如下所示:

If you know the exact id of a file (e.g. '0BxDqyd_bUCmvN1E3N0dQOWgycEF'), you can get it as a blob like this:

var picture3Blob = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycEF').getBlob();

这是一个可行的示例:

function sendPics() {
  var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
  var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
   MailApp.sendEmail({
     to: 'testa@example.com, testb@example.com', 
     subject: "This is a test", 
     body:"Test message",
     attachments: [picture1.getBlob(), picture2.getBlob()]
  });
}

这是将图片内联而不是作为附件添加的示例:

and here's an example of the pictures being added inline instead of as attachments:

function sendPicsInline() {
  var picture1 = DriveApp.getFileById('0BxDqyd_bUCmvN1E3N0dQOWgycFE'); //public with link
  var picture2 = DriveApp.getFileById('0BxDqyd_bUCmvTFNjRkRXbXA2Tms'); //public with link
  var inlineImages = {};
  inlineImages[picture1.getId()] = picture1.getBlob();
  inlineImages[picture2.getId()] = picture2.getBlob();
   MailApp.sendEmail({
     to: 'testa@example.com, testb@example.com', 
     subject: "This is a test", 
     body:"Test message",
     htmlBody: 'Test message with pics inline <br>' +
     'first:<br><img src="cid:' + picture1.getId() + '" /><br>' +
     'second:<br><img src="cid:' + picture2.getId() + '" />',
     inlineImages: inlineImages   
  });
}

这篇关于使用Google脚本发送带有图片附件和正文的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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