NEWBIE: Impossible to get my attached pdf in a mail with google script and form

I’m new to google script and I’m going crazy trying to do a simple tool.
I created a simple google form with just an email and a file uploader and I need just to insert an email and a pdf, and this should go to the recipient well HTML formatted and whit the pdf attached.
I’m new to this and I tried litterally every syntax using mailApp and Gmail app and the mail comes smooth but the pdf wont.
I know it’s just a newbie stupid thing but I can’t figure it out at my 74th attempt.
here’s the code, I will be grateful forever with someone who can help me!

function sendPDF (e) {
  var html = HtmlService.createTemplateFromFile("emailbolladireso.html");
  var htmlText = html.evaluate().getContent();
  var emailTo = e.response.getRespondentEmail();
  var Subject = "---."
  var textbody = "---."
  var attach = e.response.getItemResponses();
  var options = { htmlBody: htmlText};
  if(emailTo !== undefined){
    GmailApp.sendEmail(emailTo, Subject, textbody, options, )
    attachments: attach[1];
  }
}

26 thoughts on “NEWBIE: Impossible to get my attached pdf in a mail with google script and form”

  1. First you have to get the id of your file wich is uploaded by your form to google drive.

    You tried with:

    var attach = e.response.getItemResponses();
    

    But this will return you a object with all your answers of your form.
    From this object you have to extract the id of the uploaded PDF.

    If you know the position, for example the first question in your form is for the pdf you can access it with attach[0] if it is e.g. in second position with attach[1], cause arrays index start with 0.

    (You could look it also up with a for loop check for the name of the object.

    attach[0].getItem().getTitle()
    

    )

    With attach[0] you get the next object and from this you get with attach[0].getResponse() finaly the id of your PDF.

    attach[0].getResponse()
    

    Now you "load" the file with the given id from google Drive (Make sure you give the permissions for access GoogleDrive)

    var file = DriveApp.getFileById(attach[0].getResponse())
    

    You can now attach your file (blob) to your attachments with the right MimeType

    attachments: [file.getAs(MimeType.PDF)]
    

    You should also place the attachments in the options of your email.

    function sendPDF (e) {
      var html = HtmlService.createTemplateFromFile("emailbolladireso.html");
      var htmlText = html.evaluate().getContent();
      var emailTo = e.response.getRespondentEmail();
      var Subject = "Form with PDF"
      var textbody = "Your PDF is attached."
      var attach = e.response.getItemResponses();
    
      var file = DriveApp.getFileById(attach[0].getResponse())
    
      var options = { htmlBody: htmlText, attachments: [file.getAs(MimeType.PDF)]};
      if(emailTo !== undefined){
        GmailApp.sendEmail(emailTo, Subject, textbody, options);
      }
    }
    

    Finaly it should work…

    I tested it like this and it works for me.

    DON’T PANIC!

    Reply

Leave a Comment