通过附件发送(getFileById) [英] Sending with Attachments (getFileById)

查看:142
本文介绍了通过附件发送(getFileById)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用脚本创建了Google表单,只要有人完成表单,该脚本就会自动将数据发送到特定的电子邮件。一切正常,因为脚本做它应该做的事情。



以下是整个代码

 函数forwardEmail(e){

var forwardEmail =myemail@myemail.com;

var senderEmail = e.values [1];
var senderName = e.values [2];
var senderLocation = e.values [3];
var senderType = e.values [9];

var studentName = e.values [4];
var broadLeafId = e.values [5];
var cVueId = e.values [6];
var studentCampus = e.values [10];

var issueCategory = e.values [8];
var description = e.values [7];

var subject =FS Support:+ issueCategory + - + studentName;
var message =\\\
响应摘要:\\\
\\\
发件人信息:\\\
联系人姓名:+ senderName +
\\\
联系人电子邮件:+ senderEmail +
\\\
位置:+ senderLocation +
\\\
雇员类型:+ senderType +
\\\
\\\
学生信息:\ n学生姓名:+ studentName +
\ n BroadLeaf ID:+ broadLeafId +
\\\
CampusVue ID:+ cVueId +
\\\
学生校园:+ studentCampus +
\\\
\\\
事件信息:\ n发布类别:+ issueCategory +
\\\
评论:+ description +

MailApp.sendEmail(forwardEmail,subject,信息);

以上代码正常工作。



<现在,我想使用相同的表单/脚本来包含/发送附件。我从这篇教程中了解了这个想法: https://www.youtube.com/watch?v=f2xPgUhcPqc 。我不希望使用特定ID分配静态文件,而是希望脚本获取与表单一起上传的文件的ID,并将其包含在同一封电子邮件中。所以我修改了脚本:

  function forwardEmail(e){

var forwardEmail =myemail @ myemail.com;

var senderEmail = e.values [1];
var senderName = e.values [2];
var senderLocation = e.values [3];
var senderType = e.values [9];

var studentName = e.values [4];
var broadLeafId = e.values [5];
var cVueId = e.values [6];
var studentCampus = e.values [10];

var issueCategory = e.values [8];
var description = e.values [7];

var fileUrl = e.values [11];
var fileId = fileUrl.split('id =')[1];
Logger.log(fileId)

var subject =FS Support:+ issueCategory + - + studentName;
var message =\\\
响应摘要:\\\
\\\
发件人信息:\\\
联系人姓名:+ senderName +
\\\
联系人电子邮件:+ senderEmail +
\\\
位置:+ senderLocation +
\\\
雇员类型:+ senderType +
\\\
\\\
学生信息:\ n学生姓名:+ studentName +
\ n BroadLeaf ID:+ broadLeafId +
\\\
CampusVue ID:+ cVueId +
\\\
学生校园:+ studentCampus +
\\\
\\\
事件信息:\ n发布类别:+ issueCategory +
\\\
评论:+ description +
\\\
\\\
附件:+ fileUrl +
\\\
Google文件ID:+ fileId;

var attachment = DriveApp.getFileById(fileId);
var attachment_type = attachment.getAs(MimeType.JPEG);

MailApp.sendEmail(forwardEmail,subject,message,{attachments:[attachment_type]});

记录器部分的工作原理是能够提取ID,问题是它只是停止发送电子邮件当我添加和修改这部分时:

  var attachment = DriveApp.getFileById(fileId); 
var attachment_type = attachment.getAs(MimeType.JPEG);

MailApp.sendEmail(forwardEmail,subject,message,{attachments:[attachment_type]});

有没有人知道如何做这项工作?我正在考虑在发送邮件之前做一个时间驱动的触发器(而不是形式suhbmit)来确保文件确实位于Google Drive中。

解决方案

试试这个:

  var file = DriveApp.getFileById(FileId); 

var blob = Utilities.newBlob('在此处插入任何HTML内容','text / html','my_document.html');

MailApp.sendEmail('myemail @ myemail.com','附件示例','',{
附件:[file.getAs(MimeType.JPEG),blob]});


I created a Google form with a script that automatically sends the data to a particular email whenever someone completes the form. Everything works fine as the script does what it's supposed to do.

Here's the entire code

function forwardEmail(e) {

  var forwardEmail = "myemail@myemail.com";

  var senderEmail = e.values[1];
  var senderName = e.values[2];
  var senderLocation = e.values[3];
  var senderType = e.values[9];

  var studentName = e.values[4];
  var broadLeafId = e.values[5];
  var cVueId = e.values[6];
  var studentCampus = e.values[10];

  var issueCategory = e.values[8];
  var description = e.values[7];

  var subject = "FS Support: " + issueCategory + " - " + studentName;  
  var message = "\n Response Summary: \n\n Sender Information: \n Contact Name: " + senderName + 
      "\n Contact Email: " + senderEmail + 
      "\n Location: " + senderLocation + 
      "\n Employee Type: " + senderType + 
      "\n\n Student Information: \n Student Name: " + studentName +   
      "\n BroadLeaf ID: " + broadLeafId +
      "\n CampusVue ID: " + cVueId +
      "\n Student Campus: " + studentCampus +
      "\n\n Incident Information: \n Issue Category: " + issueCategory +   
      "\n Comments: " + description + 

 MailApp.sendEmail(forwardEmail, subject, message);

The code above works as it is.

Now, I want to use the same form/script to include/send attachments as well. I got the idea from this tutorial https://www.youtube.com/watch?v=f2xPgUhcPqc. Instead of assigning a "static" file using a specific ID, I wanted the script to get the ID of the file that is uploaded with the form and include it in the same email. So I modified the script:

function forwardEmail(e) {

  var forwardEmail = "myemail@myemail.com";

  var senderEmail = e.values[1];
  var senderName = e.values[2];
  var senderLocation = e.values[3];
  var senderType = e.values[9];

  var studentName = e.values[4];
  var broadLeafId = e.values[5];
  var cVueId = e.values[6];
  var studentCampus = e.values[10];

  var issueCategory = e.values[8];
  var description = e.values[7];

  var fileUrl = e.values[11];
  var fileId = fileUrl.split('id=')[1];
  Logger.log(fileId)

  var subject = "FS Support: " + issueCategory + " - " + studentName;  
  var message = "\n Response Summary: \n\n Sender Information: \n Contact Name: " + senderName + 
      "\n Contact Email: " + senderEmail + 
      "\n Location: " + senderLocation + 
      "\n Employee Type: " + senderType + 
      "\n\n Student Information: \n Student Name: " + studentName +   
      "\n BroadLeaf ID: " + broadLeafId +
      "\n CampusVue ID: " + cVueId +
      "\n Student Campus: " + studentCampus +
      "\n\n Incident Information: \n Issue Category: " + issueCategory +   
      "\n Comments: " + description + 
      "\n\n Attachment: " + fileUrl + 
      "\n Google Doc ID: " + fileId;

  var attachment = DriveApp.getFileById(fileId);
  var attachment_type = attachment.getAs(MimeType.JPEG);

  MailApp.sendEmail(forwardEmail, subject, message, {attachments: [attachment_type]});

The logger portion works as it is able to extract the ID, the problem is it just stops sending emails when I add and modify this portion:

  var attachment = DriveApp.getFileById(fileId);
  var attachment_type = attachment.getAs(MimeType.JPEG);

  MailApp.sendEmail(forwardEmail, subject, message, {attachments: [attachment_type]});

Does anyone have any idea on how to make this work? I am thinking of doing a "time-driven" trigger (instead of "on form suhbmit) to make sure that the file is indeed in the Google Drive before the email is sent.

解决方案

Try this:

var file = DriveApp.getFileById(FileId);

var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');

MailApp.sendEmail('myemail@myemail.com', 'Attachment example', '', {
     attachments: [file.getAs(MimeType.JPEG), blob]});

这篇关于通过附件发送(getFileById)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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