Use Google Apps Script to send an email to a specific recipient.

Objective: Use Google Apps Script to send an email to a specific recipient.

Instructions:

  • Follow the same initial steps as above to create a new Google Apps Script project.
  • Paste the code into the script editor, replacing “recipient@example.com” with your actual recipient’s email address.
  • Save and run the sendEmail function.
  • Check the recipient’s email inbox for the email.

Explanation: This script uses the MailApp.sendEmail() method to send an email. It requires the recipient’s email address, the subject of the email, and the body of the email as parameters.

Code:

function sendMyEmail(){

  const recipient = “email@gmail.com”;

  const subject = ‘Test Email’;

  const body = ‘This is the body of the email.’;

  MailApp.sendEmail(recipient,subject,body);

}

The function named sendMyEmail() that uses Google Apps Script to send an email using the MailApp.sendEmail() method. 

Let’s break down the code step by step:

  1. const recipient = “email@gmail.com”;: This line declares a constant variable named recipient and assigns the email address “email@gmail.com” to it. This is the email address of the recipient who will receive the email.
  2. const subject = ‘Test Email’;: This line declares another constant variable named subject and assigns the string ‘Test Email’ to it. This string represents the subject of the email.
  3. const body = ‘This is the body of the email.’;: This line declares a constant variable named body and assigns the string ‘This is the body of the email.’ to it. This string represents the main content or body of the email.
  4. MailApp.sendEmail(recipient, subject, body);: This line uses the MailApp.sendEmail() method provided by Google Apps Script to send an email. It takes three arguments:
    • recipient: The email address of the recipient.
    • subject: The subject of the email.
    • body: The body/content of the email.

When you execute the sendMyEmail() function, it will send an email to the specified recipient with the subject “Test Email” and the body text “This is the body of the email.”

This is a simple example of how to send an email using Google Apps Script. You can customize the recipient, subject, and body of the email to send more meaningful and relevant messages programmatically. This can be particularly useful for automating email notifications, reports, or other communication tasks within your Google Apps Script projects.