Skip to main content

Send Bulk Emails with #GAS



In my attempt to explain and demonstrate the usefulness of #GAS, we have taken a look at how to use it to: Add / Delete Calendar Events; Add Custom Menu Buttons to our functions. We will now take a look at how to Send Bulk Emails using Google Apps Script. Please don't spam people with this!!!

The example code we will be going through allows you to send bulk emails at specific times (if you want to): daily/weekly/hourly. We will be using two main services for this exercise; Spreadsheet Services and Mail Services from the default services Google Apps Script provides. The guide require about 15 minutes to go through and try it out.

Make a copy of the spreadsheet we will be going from here. If you are not familiar with how to write or get around Google Apps Script, please pause, read these posts; Google Apps Script and Writing a Google Apps Script and come back.

Setup
  1. Open the copy of the spreadsheet you just made.
  2. Update the variables
    • Cell D2 - This holds the name of the Email Sender. This is the name that will appear besides your email address in the receivers' inbox.
    • Row A6:G - Each row from this point below holds the contact information and the messages we want to send to each recipient. You can add more rows from row 6 with the necessary information. Column H by default will be empty, but if the sendMail function runs perfectly, it will be updated by our script to "Mail Sent"
  3. On the menu (from the script editor), click execute->addMenuItemSendEmails to create a new menu item in your spreadsheet UI. 
  4. You can set-up time-driven triggers to have the script run at specific times that you want. 
  5. In your Spreadsheet UI, locate the Execute Menu and click on Send Emails. This will go through all the variables you set up from  Row 6  and generate a custom email to them. 

The Code

The function doing the magic (not much of magic) here is sendEmails. The function basically goes through the various variables we have in our spreadsheet to generates the emails.

Line 8 - 13 uses the Spreadsheet Class from Spreadsheet Services to get the sheet that we want to work with, in the case "Variables".

On line 30, we use sendEmail from the MaileApp (Mail Services) class to send the emails by providing the required parameters. The class uses your logged in account to send the email, hence if any of the emails should bounce, you will receive an email notification of the bounced messages.

If you used a time-driven trigger, please do not forget to turn them off when you don't need to send the emails. Else you will spam your recipients. 

Comments

Popular posts from this blog

EC isn't prepared to take technology to the next level in Elections

In Election 2012, Ghana's EC introduces a new process to verify voters before they are allowed to cast their vote. This was in the form of biometric verification, a good idea but poorly not so well implemented.

Be a part of the experience, and make sure your Polling Station is represented on Google Maps!

Excited to see the number of polling stations that have been mapped by Ghanaian voters. It is validation of the belief in the internet’s role in helping to ensure that the 2012 elections will be the best ever held in Ghana.