How to Automate Emails with Google Apps Script in Gmail

Managing repetitive email tasks manually is a drain on productivity. Whether you need to send daily status updates, distribute weekly reports, or trigger alerts based on specific conditions, relying on human memory is inefficient. For Google Workspace users, the most powerful tool for solving this problem is Google Apps Script. This built-in JavaScript platform allows you to interact directly with your Gmail account and automate complex communication workflows entirely in the background.

Why Apps Script is Better Than Standard Filters

While standard Gmail filters are excellent for organising incoming mail, they cannot generate new outgoing messages on a schedule or dynamically pull data from other sources. Google Apps Script bridges this gap. Because it runs on Google’s secure servers, your automated emails will continue to send reliably even when your computer is turned off.

What You Need Before Starting

You do not need to be an expert programmer to use Apps Script, but basic familiarity with spreadsheets and simple logic is helpful. You will need a standard Google Account or a Google Workspace account. All the tools required are free and accessible directly from your browser.

Accessing the Google Apps Script Editor

The script editor is where you will write and deploy your automation code.

  1. Open your web browser and navigate to script.google.com.
  2. Log in with your Google credentials if prompted.
  3. Click the New project button in the top left corner of the dashboard.
  4. Click on “Untitled project” at the top of the screen and rename it to something recognizable, such as “Gmail Automations”, then click Rename.

You are now looking at the code editor, which contains a blank function named myFunction().

Writing the Email Automation Script

To automate an email, you will use the built-in MailApp service provided by Google. The syntax is straightforward.

  1. Delete the existing code in the editor and replace it with the following script:
function sendAutomatedEmail() {
  var recipient = "[email protected]";
  var subject = "Automated Weekly Update";
  var body = "Hello,\n\nThis is an automated email generated by Google Apps Script.\n\nBest regards,\nYour Name";
  
  MailApp.sendEmail(recipient, subject, body);
}
  1. Modify the recipient, subject, and body variables to suit your needs. The \n characters create line breaks in the email body.
  2. Click the Save project icon (the floppy disk) in the toolbar.

Testing and Authorising the Script

Before you can automate the script, you must run it manually once to grant the necessary security permissions.

  1. Click the Run button in the toolbar.
  2. A security prompt titled “Authorization required” will appear. Click Review permissions.
  3. Select your Google account.
  4. Google will display a warning stating that the app isn’t verified. This is normal for scripts you create yourself. Click Advanced at the bottom, then click Go to Gmail Automations (unsafe).
  5. Review the permissions requested (sending email on your behalf) and click Allow.

The script will execute, and the email will be sent immediately to the recipient you specified.

Scheduling the Automated Email

The true power of Apps Script lies in its ability to run on a schedule using time-driven triggers.

  1. In the Apps Script editor, look at the left-hand sidebar and click the Triggers icon (it looks like a clock).
  2. Click the blue Add Trigger button in the bottom right corner.
  3. Under “Choose which function to run”, ensure sendAutomatedEmail is selected.
  4. Under “Select event source”, choose Time-driven.
  5. Under “Select type of time based trigger”, choose your preferred schedule (e.g., Week timer).
  6. Under “Select time of day”, choose when the email should be sent.
  7. Click Save.

Google will now automatically run your script and send the email in the background according to your exact schedule, drastically improving your productivity.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.