Few years ago I had a lot of fun with Google App Scripts.
Below is the code to automatically create a PDF from a Google document and send it via email; you find the instructions and code below and in the GitHub repository.
- Open a new Google document
- Click on ‘Tools > Script editor’
- Copy the code below in new scripts files inside your new App Script project
- In the pdf.gs code, substitute:
- [INSERT EMAIL] at line 12 with the recipient / recipients of the email (if multiple, separate emails with comma)
- [INSERT IMAGE URL] at line 13 with the URL of the image you want to include in the email body after text
- [INSERT EMAIL SUBJECT] at line 16 with the desired email subject
- The text of the string at line 17 with your own text; this will be the email content, the HTML tag serves as line break
- [EMAIL SENDER VISUALIZED NAME] at line 20 with the desired sender’s name
- “Hello” at line 26 with the desired fallback body text, for devices incapable of rendering HTML
- Save all files in the App Script project and close it
- Refresh the document page, update its content and click on the menu to ‘Email > Send PDF’
- Your document will automatically be exported in PDF and sent to the recipients specified in pdf.gs.
pdf.gs
function pdf() {
DocumentApp.getActiveDocument().saveAndClose();
// Create a PDF from the Google document
var doc = DocumentApp.getActiveDocument();
var ui = DocumentApp.getUi();
var docblob = DocumentApp.getActiveDocument().getAs('application/pdf');
docblob.setName(doc.getName() + ".pdf");
// Prepare and send the email from the connected Gmail account
var recipients = "[INSERT EMAIL]";
var ds = "[INSERT IMAGE URL]";
var ds2 = UrlFetchApp.fetch(ds).getBlob();
var subject = "[INSERT EMAIL SUBJECT]";
var string = "Hi Mark,<br><br> Please find attached the requested pdf.<br><br>Thanks,<br>Federico";
var options = {};
options.name = "[EMAIL SENDER VISUALIZED NAME]";
options.replyTo = recipients;
options.htmlBody = string + "<br><br><img src='cid:firma'>";
options.inlineImages = {firma:ds2};
options.attachments = [docblob];
MailApp.sendEmail(recipients, subject, "Hello", options);
}menu.gs
function onOpen(e) {
// Create the menu item in the Google document
DocumentApp.getUi()
.createMenu('Email')
.addItem('Send PDF', 'pdf')
.addToUi();
}
Thank you, your comments made it very easy to get it running.
My problem is, the recipient email address is actually in the text document that will be sent: “email: xxxx@gmail.com”
I tried to find a way to find it and read it into the “var recipients =”
If you have any idea how it could be done, I would appreciate it. Mike
Hi Michael, thanks for your message.
This can be useful to you: https://stackoverflow.com/questions/14440444/extract-all-email-addresses-from-bulk-text-using-jquery.
Nice Work man. I have one question. How can i send email with pdf attachment that is in a google sheet as a google drive link. How can i fetch that pdf from that link and attach with email?
Thanks in advance.
Thank you!
You’d need to learn how to grab the link for a Google Sheet, this should help: https://blog.gsmart.in/google-sheet-script-get-cell-value/.