0

我已经使用以下代码设置了一个触发器,以在员工每次提交请求时发送一​​封电子邮件。这基本上是发送一个小册子(PNG 格式并存储在 Google Drive 中)。不知何故,代码不起作用。几乎一整天都在网上搜寻答案,但没有任何帮助。我尝试了 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());
    }

}
4

1 回答 1

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());   
}
于 2013-11-13T19:40:37.183 回答