Send Emails with Attachments with Google Apps Script and Mandrill

Gmail sending limits are restricted especially when you are sending emails programatically as in the case of Mail Merge. You can only send a few hundred emails per day and then you’ve to wait the entire day for Google to reset your limit.

If you would like to send thousands of emails but without the daily restrictions, you can consider using an email sending service like Mandrill. You need a web domain and need to verify the ownership of that domain with Mandrill so that you are allowed to send emails via Mandrill. Once verified, you get an API key from Mandrill that you can use with Apps Script to send emails in bulk without restrictions.

Here’s a sample snippet that sends emails from Google Scripts via Mandrill. You also have to option to include file attachments in your emails and these files can be placed in a Google Drive folder. The sendEmail() method accepts both plain text and HTML Mail.

function sendEmail() {
  var MANDRILL_API_KEY = '<<your key here>>';

  var files = ['<<Google Drive File ID 1>>', '<<Google Drive File ID 2>>', '<<Google Drive File ID 3>>'];

  var recipients = [
    {
      email: 'ctrlq+to@labnol.org',
      name: 'Amit Agarwal',
      type: 'to',
    },
    {
      email: 'ctrlq+cc@labnol.org',
      type: 'cc',
    },
    {
      email: 'ctrlq+bcc@gmail.com',
      type: 'bcc',
    },
  ];

  var attachments = [];

  for (var f in files) {
    var file = DriveApp.getFileById(files[f]);
    attachments.push({
      type: file.getMimeType(),
      name: file.getName(),
      content: Utilities.base64Encode(file.getBlob().getBytes()),
    });
  }

  var params = {
    key: MANDRILL_API_KEY,
    message: {
      from_email: "<<Sender's Email Address>>",
      from_name: '<<Sender Name>>',
      to: recipients,
      attachments: attachments,
      headers: {
        'Reply-To': 'reply@example.com',
      },
      subject: 'Enter email subject',
      text: 'Enter email body in plain text',
      html: 'Enter HTML content with <b>tags</b>',
    },
  };

  var response = UrlFetchApp.fetch('https://mandrillapp.com/api/1.0/messages/send.json', {
    method: 'POST',
    payload: JSON.stringify(params),
    contentType: 'application/json',
  });

  Logger.log(response.getContentText());
}

It may take some time to build your mail domain reputation and hence the emails are queued and not sent instantly. Go to Mandrill Dashboard - Outbound Emails - Activity to see the current status of your sent Emails.

Mandrill Email Open Report

Also, it is no longer possible to send emails from generic addresses like @gmail.com or @outlook.com since Mandrill requires domain ownership verification to reduce spam emails.

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.