将两个不同的PDF附加到电子邮件 [英] Attach two different pdfs to email

查看:75
本文介绍了将两个不同的PDF附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Google表单收到订单.我会自动发送一封带有订单的(a)个性化pdf的电子邮件,但我也想在来自Google云端硬盘文件夹的电子邮件中添加(b)pdf(条款和服务,所有订单均相同).

(A)工作正常,但我不确定如何添加(B).

这是个性化的pdf:

 函数afterFormSubmit(e){const info = e.namedValues;const pdfFile = createPDF(info);sendEmail(e.namedValues ['email'] [0],pdfFile);}函数sendEmail(email,pdfFile){GmailApp.sendEmail(电子邮件,收到的订单",请在附件中找到您的订单和服务条款",{附件:[pdfFile],名称:"MyCompany"});}函数createPDF(info){const pdfFolder = DriveApp.getFolderById("XXXXXXXXX");const tempFolder = DriveApp.getFolderById("XXXXXXXXX");const templateDoc = DriveApp.getFileById("XXXXXXXXX");const newTempFile = templateDoc.makeCopy(tempFolder);const openDoc = DocumentApp.openById(newTempFile.getId());const body = openDoc.getBody();body.replaceText("{fn}",info ['名字'] [0]);body.replaceText("{ln}",info ['姓氏'] [0]);openDoc.saveAndClose();const blobPDF = newTempFile.getAs(MimeType.PDF);const pdfFile = pdfFolder.createFile(blobPDF).setName(info ['First name'] [0] +" + info ['Last name'] [0]);;tempFolder.removeFile(newTempFile);返回pdfFile;} 

所以我创建了 const pdfTerms = DriveApp.getFileById("XXXXXXXXX");,我已将其添加到 sendEmails 附件:[pdfFile,pdfTerms] 中,但是它不起作用.

解决方案

说明:

问题在于您采用的方法 pdfTerms 不是 pdf ,而是

始终假定存在具有此特定ID的文件.

解决方案:

修改 afterFormSubmit sendEmail 如下:

 函数afterFormSubmit(e){const info = e.namedValues;const pdfFile = createPDF(info);const pdfTerms_file = DriveApp.getFileById("XXXXXXXXX");//文件类型const pdfTerms = pdfTerms_file.next().getAs(MimeType.PDF);//pdfsendEmail(e.namedValues ['email'] [0],pdfFile,pdfTerms);}函数sendEmail(email,pdfFile,pdfTerms){GmailApp.sendEmail(电子邮件,收到的订单",请在附件中找到您的订单和服务条款",{附件:[pdfFile,pdfTerms],名称:"MyCompany"});} 

参考:

I receive an order via Google Form. I send an automatic email with the (a) personalised pdf with the order, but I also want to add a (b) pdf to the email coming from a Google Drive folder (terms and services, the same for all orders).

(A) is working fine, but I am not sure how to add (B).

This is the personalised pdf:

function afterFormSubmit(e) {
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  sendEmail(e.namedValues['email'][0],pdfFile);
}
function sendEmail(email,pdfFile){
  GmailApp.sendEmail(email, "Order received","Please find attached your order and terms of services", {
    attachments: [pdfFile],
    name: 'MyCompany'
  });
}
function createPDF(info) {
const pdfFolder = DriveApp.getFolderById("XXXXXXXXX");  
const tempFolder = DriveApp.getFolderById("XXXXXXXXX");  
const templateDoc = DriveApp.getFileById("XXXXXXXXX");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{fn}", info['First name'][0]);
body.replaceText("{ln}", info['Last name'][0]);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['First name'][0] + " " + info['Last name'][0]);
tempFolder.removeFile(newTempFile);
return pdfFile;
}

So I have created const pdfTerms = DriveApp.getFileById("XXXXXXXXX"); and I have added it to attachments: [pdfFile,pdfTerms] of sendEmails however it does not work.

解决方案

Explanation:

The issue is that pdfTerms in your approach is not a pdf but an object of type file.

To convert the file to a pdf you need to do that:

const pdfTerms_file = DriveApp.getFileById("XXXXXXXXX"); // type of file
const pdfTerms = pdfTerms_file.next().getAs(MimeType.PDF); // pdf

always assuming that there is a file with this particular id.

Solution:

Modify afterFormSubmit and sendEmail as follows:

function afterFormSubmit(e) {
  const info = e.namedValues;
  const pdfFile = createPDF(info);
  const pdfTerms_file = DriveApp.getFileById("XXXXXXXXX"); // type of file
  const pdfTerms = pdfTerms_file.next().getAs(MimeType.PDF); // pdf
  sendEmail(e.namedValues['email'][0],pdfFile,pdfTerms);
}
function sendEmail(email,pdfFile,pdfTerms){
  GmailApp.sendEmail(email, "Order received","Please find attached your order and terms of services", {
    attachments: [pdfFile,pdfTerms],
    name: 'MyCompany'
  });
}

References:

这篇关于将两个不同的PDF附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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