Automatically send a Google document as PDF attachment by email with Apps Script

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.

  1. Open a new Google document
  2. Click on ‘Tools > Script editor’
  3. Copy the code below in new scripts files inside your new App Script project
  4. 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
  5. Save all files in the App Script project and close it
  6. Refresh the document page, update its content and click on the menu to ‘Email > Send PDF’
  7. 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();
 }

Federico

4 Comments

  1. 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

  2. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *