我试图自动保存在GMail中收到的收据(从亚马逊)到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('myemail@gmail.com',
'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]);
}
发布于 2022-02-21 11:03:21
据谷歌开发人员称,页面 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('myemail@gmail.com',
'transfer email as pdf : body & attachment',
'see attachment', {
attachments: attachmentList
});
// Message Processed; Remove the Google Drive Label
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(thread[x]);
}
}
发布于 2013-06-05 20:39:54
这段代码是我不久前写的,如果攻击是以一种可以转换成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('xxxxxxx@gmail.com', 'transfer email as pdf : body ', 'see attachment', {attachments:[body_to_send]});
MailApp.sendEmail('xxxxxxx@gmail.com', 'transfer email as pdf : body & attachment', 'see attachment', {attachments:[attach_to_send,body_to_send]});
file.setTrashed(true);
DocsList.getFileById(bodyId).setTrashed(true)
}
}
https://stackoverflow.com/questions/16946168
复制相似问题