Google脚本 - 无法从Google云端硬盘附加PNG文件以形成电子邮件

时间:2013-11-13 15:36:51

标签: google-apps-script jscript

我设置了一个触发器,其中包含以下代码,以便每次员工提交请求时都会发送一封电子邮件。这基本上做的是发送一本小册子(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());
    }

}

1 个答案:

答案 0 :(得分:0)

尝试将图像作为具有正确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());   
}