1

I have a script that sends an email. I would like to set it up so that the email is sent with a pdf attachment that is in my google drive. The name of the file is pdfholder.pdf

Here is the code that is currently working (without attachment), and sending emails

MailApp.sendEmail(userEmail, subject, message);

Here is the code that is not working (with the attachment), and not sending emails

var file = DocsList.getFileById('pdfholder');
MailApp.sendEmail(userEmail, subject, message, {attachments:file});

Any ideas on how to get this working? I am new to google apps scripting so simple/thorough explanations would be much appreciated. Thanks!

2 Answers 2

4

The argument needed for the optional argument attachment is an array (as clearly shown in the documentation). This is to allow for easy handling of multiple attached files. In you case it will be an array of one single element : [file] so your final code would (indeed) be

MailApp.sendEmail(userEmail, subject, message, {attachments:[file]});
Sign up to request clarification or add additional context in comments.

2 Comments

"no offense but this kind of question needs more than just a line of code... a word of explanation and/or a link to the doc is IMHO often necessary for "non-expert" askers." – Serge insas / William Chiquito says: Totally agree. Unfortunately I did not count with enough time to give a more elaborate answer to the question, so I put the code only. On future occasions will avoid this type of answers. Thank you. I deleted my answer.
I am using GmailApp. Everything works well , but when we download the attached file. it is downloaded as "File Type" rather "PDF type". What can be the issue?
2

The official doc has an example exactly for this purpose too:

// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
 var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
 var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
 MailApp.sendEmail('[email protected]', 'Attachment example', 'Two files are attached.', {
     name: 'Automatic Emailer Script',
     attachments: [file.getAs(MimeType.PDF), blob]
 });

See: https://developers.google.com/apps-script/reference/mail/mail-app

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.