简体   繁体   English

Google Apps脚本:使用mailapp.sendmail服务发送仅带有偶尔附件的邮件

[英]Google apps script: use the mailapp.sendmail service to send mails with only occasional attachments

Please be patient, I am new to GAS. 请耐心等待,我是GAS的新手。 I am trying to use the mailapp.sendmail service to send mails with only occasional attachments. 我正在尝试使用mailapp.sendmail服务发送仅带有偶尔附件的邮件。 Somehow it ends with either the variant without attachment works or the one with attachment, but never both of them. 不知何故,它要么以不带附件的变体结束,要么以带附件的变体结束,但决不会两者都结束。 Here is the code: 这是代码:

function sendMail(tomail, subject, fill_cell, messageTemplate, EMAIL_SENT, fileAttach) {
  if (fileAttach == "noFile" )
     { var fileAttachHolder = ' ' }
   else
     { var fileAttachHolder = fileAttach + '.getAs(MimeType.PDF) '};   
  MailApp.sendEmail(tomail, subject,'', {htmlBody: messageTemplate, attachments: [fileAttachHolder]});
  fill_cell.setValue(EMAIL_SENT);
  SpreadsheetApp.flush();
  // Browser.msgBox('Mail sent: '+ subject);
}

calling the function like this: 像这样调用函数:

 if ((emailSent != EMAIL_SENT)  && row[4] > now ) {  // Prevents sending duplicates or outdated
  sendMail(
    emailAddress,
    "Hi, " + row[1] + " " + row[3],
    sheet.getRange(startRow + i, 18),
    HMESSAGE,
    EMAIL_SENT            
  );

I guess it has to do with how I define the attachment variable. 我想这与我如何定义附件变量有关。 Grateful for help. 感谢帮助。 Martin 马丁

An attachments must be in your drive and this file is a blobType. 附件必须在驱动器中,并且此文件是blobType。

I can show you an example of my script. 我可以向您展示我的脚本示例。

function sendEmail() {

var Rainfall = DriveApp.getFilesByName('Untitled document')
var Rainfall2 = DriveApp.getFilesByName('128x128_tl_icon.png')

MailApp.sendEmail({
to:"mymail@gmail.com", 
subject: "Images for Social Media", 
body:"Hi Joe, here are the images for Social Media",


attachments: [Rainfall.next(), Rainfall2.next()]
  })

}
sendEmail()

finally found a solution, I am sure not the most elegant but it works: 终于找到了解决方案,我敢肯定不是最优雅的,但是它可以工作:

    function sendMail(tomail, subject, fill_cell, messageTemplate, EMAIL_SENT) {  
  MailApp.sendEmail(tomail, subject,'', {htmlBody: messageTemplate});
  fill_cell.setValue(EMAIL_SENT);
  SpreadsheetApp.flush();

} }

    function sendMailwAtt(tomail, subject, fill_cell, messageTemplate, EMAIL_SENT, fileAttach) {  
  MailApp.sendEmail(tomail, subject,'', {htmlBody: messageTemplate, attachments: [fileAttach.next()]});
  fill_cell.setValue(EMAIL_SENT);
  SpreadsheetApp.flush();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM