Question

I've setup a trigger with the following code to send out an email everytime an employee submits a request. What this basically does is send out a brochure (in PNG format and stored in Google Drive). Somehow, the code does not work. Spent almost the entire day scouring the net for answers, but none helped. I tried the DriveApp.getFileByID method and it worked, but that was just for one file. The attachment depends on the product that the employee selects on the Google form, and that is dynamic.

Any help is much appreciated. Thanks!

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());
    }

}
Was it helpful?

Solution

try getting the image as a blob with the right mime type, see [doc here][1],

in your code it would become :

...
attachments: [brochure1.next().getAs('image/png')],
...

PS : didn't have the opportunity to test this, please let us know it it does work. [1]: https://developers.google.com/apps-script/reference/drive/file?hl=fr-FR#getAs%28String%29


EDIT : here is a new version of your code that works, just change the image file names to suit your case.

I wrote a test function to be able to test without form submission.

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());   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top