我正在尝试将 GMail 中收到的收据(来自 Amazon)自动保存到 Dropbox。所以我写了一个脚本:
脚本可以运行并生成 bodydochtml,但 PDF 转换和电子邮件不起作用。我盯着这个剧本看了好几个小时。我的脚本哪里有错误?
谢谢!
Function send_Gmail_as_PDF(){
var gLabel = "#Receipt";
var thread = GmailApp.search("label:" + gLabel);
for (var x=0; x<thread.length; x++) {
var messages = thread[x].getMessages();
for (var y=0; y<messages.length; y++) {
var attach = messages[y].getAttachments();
var body = messages[y].getBody();
// Create an HTML File from the Message Body
var bodydochtml = DocsList.createFile('body.html', body, "text/html")
var bodyId=bodydochtml.getId()
// Convert the HTML to PDF
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
// Does the Gmail Message have any attachments?
if(attach.length>0){
var file=DocsList.createFile(attach[0]);
var pdf=file.getAs('application/pdf').getBytes();
var attach_to_send = {fileName: 'pdftest.pdf',
content:pdf, mimeType:'application/pdf'};
var body_to_send = {fileName: 'body.pdf',
content:bodydocpdf, mimeType:'application/pdf'};
// Send the PDF to any email address
MailApp.sendEmail('[email protected]',
'transfer email as pdf : body & attachment',
'see attachment', {attachments:[attach_to_send,body_to_send]});
// Trash the temporary PDF and HTML files
file.setTrashed(true);
DocsList.getFileById(bodyId).setTrashed(true)
}
}
}
// Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
当
thread[x]
为空时,会出现第 46 行的错误。由于您在处理 thread[x] 的循环之外有该语句,因此您始终为 null。将语句移到循环中,就避免了这个问题。
在消息循环中,您检查消息是否有任何附件,
if(attach.length>0){
,只有在有附件时才继续处理消息。您不想继续发送仅包含 pdf 正文的电子邮件吗?如果是这样,我们需要对{attachments:[attach_to_send,body_to_send]}
中的固定数组做一些事情。更好的方法是从 body_to_send
开始构建一系列附件。添加:
var attachmentList = [];
attachmentList.push(body_to_send);
更好的是,如果邮件有多个附件怎么办 - 您想要全部附件。为此,请将附件处理放入循环内,而不是
if
,并确保将临时文件与其一起整理。 (无论如何,它应该在 if
内部,因为如果没有附件,setTrashed()
调用就会崩溃。)
// Process all attachments
for (var att = 0; att < attach.length; att++) {
...
attachmentList.push(attach_to_send);
// Trash the temporary file
file.setTrashed(true);
}
这是您的代码,经过这些更改 - 它运行良好:
function send_Gmail_as_PDF() {
var gLabel = "#Receipt";
var thread = GmailApp.search("label:" + gLabel);
for (var x = 0; x < thread.length; x++) {
var messages = thread[x].getMessages();
for (var y = 0; y < messages.length; y++) {
var attach = messages[y].getAttachments();
var body = messages[y].getBody();
// Create an HTML File from the Message Body
var bodydochtml = DocsList.createFile('body.html', body, "text/html")
var bodyId = bodydochtml.getId()
// Convert the HTML to PDF
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
var body_to_send = {
fileName: 'body.pdf',
content: bodydocpdf,
mimeType: 'application/pdf'
};
var attachmentList = [];
attachmentList.push(body_to_send);
// Trash the temporary file
bodydochtml.setTrashed(true);
// Process all attachments
for (var att = 0; att < attach.length; att++) {
var file = DocsList.createFile(attach[att]);
var pdf = file.getAs('application/pdf').getBytes();
var attach_to_send = {
fileName: 'pdftest.pdf',
content: pdf,
mimeType: 'application/pdf'
};
attachmentList.push(attach_to_send);
// Trash the temporary file
file.setTrashed(true);
}
}
// Send the PDF to any email address
MailApp.sendEmail('[email protected]',
'transfer email as pdf : body & attachment',
'see attachment', {
attachments: attachmentList
});
// Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
}
代码已通过美化器。
根据 Google 开发人员的说法,page DocsList 已被弃用。
上面答案中的代码应该使用 DriveApp 代替。
function send_Gmail_as_PDF() {
var gLabel = "#Receipt";
var thread = GmailApp.search("label:" + gLabel);
for (var x = 0; x < thread.length; x++) {
var messages = thread[x].getMessages();
for (var y = 0; y < messages.length; y++) {
var attach = messages[y].getAttachments();
var body = messages[y].getBody();
// Create an HTML File from the Message Body
var bodydochtml = DriveApp.createFile('body.html', body, "text/html")
var bodyId = bodydochtml.getId()
// Convert the HTML to PDF
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
var body_to_send = {
fileName: 'body.pdf',
content: bodydocpdf,
mimeType: 'application/pdf'
};
var attachmentList = [];
attachmentList.push(body_to_send);
// Trash the temporary file
bodydochtml.setTrashed(true);
// Process all attachments
for (var att = 0; att < attach.length; att++) {
var file = DriveApp.createFile(attach[att]);
var pdf = file.getAs('application/pdf').getBytes();
var attach_to_send = {
fileName: 'pdftest.pdf',
content: pdf,
mimeType: 'application/pdf'
};
attachmentList.push(attach_to_send);
// Trash the temporary file
file.setTrashed(true);
}
}
// Send the PDF to any email address
MailApp.sendEmail('[email protected]',
'transfer email as pdf : body & attachment',
'see attachment', {
attachments: attachmentList
});
// Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
}
感谢所有脚本,我刚刚意识到电子邮件线程未标记为已读。
Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
似乎不起作用。
我尝试添加 thread.markRead() 但什么也没有 有什么想法吗?
如果附件的格式可以转换为 pdf,我不久前编写的这段代码就可以完成这项工作。 它在最后一个线程中获取消息,并仅在存在附件时才发送正文和附件。 这是一个测试脚本。
function getAttachAndBody(){
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var attach = message.getAttachments();
var body = message.getBody();//is a string
var bodydochtml = DocsList.createFile('body.html', body, "text/html")
var bodyId=bodydochtml.getId()
var bodydocpdf = bodydochtml.getAs('application/pdf').getBytes();
if(attach.length>0){
var file=DocsList.createFile(attach[0])
var pdf=file.getAs('application/pdf').getBytes();
var attach_to_send = {fileName: 'pdftest.pdf',content:pdf, mimeType:'application/pdf'};
var body_to_send = {fileName: 'body.pdf',content:bodydocpdf, mimeType:'application/pdf'};
// MailApp.sendEmail('[email protected]', 'transfer email as pdf : body ', 'see attachment', {attachments:[body_to_send]});
MailApp.sendEmail('[email protected]', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]});
file.setTrashed(true);
DocsList.getFileById(bodyId).setTrashed(true)
}
}