[英]Attach a pdf from google drive to an email using google apps script
[英]Google script - Can't attach a PNG file from Google Drive to form email
我使用以下代碼設置了觸發器,以便在員工每次提交請求時發送電子郵件。 這基本上是發送一本小冊子(以PNG格式並存儲在Google雲端硬盤中)。 不知何故,代碼無法正常工作。 幾乎整天都在網上搜尋答案,但沒有一個幫助。 我嘗試了DriveApp.getFileByID方法,它起作用了,但這僅適用於一個文件。 附件取決於員工在Google表單上選擇的產品,並且該產品是動態的。
任何幫助深表感謝。 謝謝!
function SendBrochure(e) {
try {
var email = e.values[1];
var subject = "ABC Co - Information you requested";
var msgcc = "test@gmail.com";
var aliases = GmailApp.getAliases();
var cxnm = e.values[2] + " " + e.values[3] + " " + e.values[4];
var msgl1 = "Greetings from ABC Co, " + cxnm + ".";
var emailText = "<b>Greetings from ABC Co, " + cxnm + ". </b> <p>With reference to your conversation
with us and your request for information, please find attached herein the same.</p> <p> Should you
require further assistance, please contact the undersigned, or refer to the brochure for pertinent
site contact details.</p> <p>We appreciate your interest and look forward to your visit soon.</p>
<p> Thanks and regards, <br><b>ABC Co</b><br>Tel: +1 202 555 1212<br>Web:
www.abc.com </p>";
var brochurename = e.values[5]; //gets file name from form (already filled in - for e.g f_1.png)
var brochure1 = DriveApp.getFilesByName(brochurename);
GmailApp.sendEmail(email, subject, msgl1, {
name: 'ABC Co',
attachments: [brochure1.next()],
htmlBody: emailText,
from: aliases[1]
});
GmailApp.sendEmail(msgcc, "Email sent!", "Your email request to " + email + " has been completed successfully at " + e.values[0], {
'from': aliases[1]
});
} catch (e) {
Logger.log(e.toString());
}
}
嘗試將圖像作為具有正確mime類型的Blob獲取,請參見[doc here] [1],
在您的代碼中它將變成:
...
attachments: [brochure1.next().getAs('image/png')],
...
PS:沒有機會進行測試,請讓我們知道它確實有效。 [1]: https : //developers.google.com/apps-script/reference/drive/file?hl = fr-FR#getAs%28String%29
編輯 :這是可以使用的代碼的新版本,只需更改圖像文件名以適合您的情況。
我編寫了一個測試功能,無需進行表單提交即可進行測試。
function test(){
var par = {};
par['values']={};
par.values[0]=new Date();
par.values[1]='xxxxxxx@gmail.com';
par.values[2]='xxxxx';
par.values[3]='yyyyy';
par.values[4]='zzzzz';
par.values[5]='LOGO.jpg';// just for test I took a file I had in my drive
SendBrochure(par);
}
function SendBrochure(e) {
Logger.clear();
var email = e.values[1];
var subject = "ABC Co - Information you requested";
var msgcc = "yyyyyyyy@gmail.com";
var aliases = GmailApp.getAliases();
var cxnm = e.values[2] + " " + e.values[3] + " " + e.values[4];
var msgl1 = "Greetings from ABC Co, " + cxnm + ".";
var emailText = "<b>Greetings from ABC Co, " + cxnm + ". </b> <p>With reference to your conversation "+
"with us and your request for information, please find attached herein the same.</p> <p> Should you "+
"require further assistance, please contact the undersigned, or refer to the brochure for pertinent "+
"site contact details.</p> <p>We appreciate your interest and look forward to your visit soon.</p>"+
"<p> Thanks and regards, <br><b>ABC Co</b><br>Tel: +1 202 555 1212<br>Web: www.abc.com </p>";
var brochurename = e.values[5]; //gets file name from form (already filled in - for e.g f_1.png)
var brochure = false;
Logger.log('brochurename = '+brochurename)
var brochure1 = DriveApp.getFilesByName(brochurename);
if(brochure1.hasNext()){
var brochure = brochure1.next().getBlob();// get the blob
Logger.log('brochure name = '+brochure.getName())
}else{
Logger.log("didn't find a file with name = "+brochurename)
}
Logger.log('email data = '+email+' '+subject+' '+ msgl1)
if(brochure){
GmailApp.sendEmail(email, subject, msgl1,{attachments:[brochure], htmlBody:emailText});
GmailApp.sendEmail(msgcc, "Email sent!", "Your email request to " + email + " has been completed successfully at " + e.values[0])
}
GmailApp.sendEmail(msgcc, "Email not sent!", "Your email request to " + email + " has not been sent because of an invalid attachment\nSee Log below : \n\n"+Logger.getLog());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.