Google Apps Script

Class MailApp

Sends email.

This service allows users to send emails with complete control over the content of the email. Unlike GmailApp, MailApp's sole purpose is sending email. MailApp cannot access a user's Gmail inbox.

Changes to scripts written using GmailApp are more likely to trigger a re-authorization request from a user than MailApp scripts.

Methods

MethodReturn typeBrief description
getRemainingDailyQuota()IntegerReturns the number of remaining emails a user can send for the rest of the day.
sendEmail(message)voidSends an email message.
sendEmail(recipient, subject, body)voidSends an email message.
sendEmail(recipient, subject, body, options)voidSends an email message with optional arguments.
sendEmail(to, replyTo, subject, body)voidSends an email message.

Detailed documentation

getRemainingDailyQuota()

Returns the number of remaining emails a user can send for the rest of the day. Quotas are based on the number of email recipients. Specific quota information is available on the quota tab of the Apps Script dashboard.

 
var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
 Logger.log("Remaining email quota: " + emailQuotaRemaining);
 

Return

Integer — the number of emails remaining that the script can send


sendEmail(message)

Sends an email message. This variation of the method is much more flexible, allowing for many more options.

 
// This code fetches the Google and YouTube logos, inlines them in an email
 // and sends the email
 function inlineImage() {
   var googleLogoUrl = "http://www.google.com/intl/en_com/images/srpr/logo3w.png";
   var youtubeLogoUrl =
         "https://developers.google.com/youtube/images/YouTube_logo_standard_white.png";
   var googleLogoBlob = UrlFetchApp
                          .fetch(googleLogoUrl)
                          .getBlob()
                          .setName("googleLogoBlob");
   var youtubeLogoBlob = UrlFetchApp
                           .fetch(youtubeLogoUrl)
                           .getBlob()
                           .setName("youtubeLogoBlob");
   MailApp.sendEmail({
     to: "recipient@example.com",
     subject: "Logos",
     htmlBody: "inline Google Logo<img src='cid:googleLogo'> images! <br>" +
               "inline YouTube Logo <img src='cid:youtubeLogo'>",
     inlineImages:
       {
         googleLogo: googleLogoBlob,
         youtubeLogo: youtubeLogoBlob
       }
   });
 }
 

Parameters

NameTypeDescription
messageObjecta JavaScript object representing an email message

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email
bccStringa comma-separated list of email addresses to BCC
bodyStringthe body of the email
ccStringa comma-separated list of email addresses to CC
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" /> (see example)
nameStringthe name of the sender of the email (default: the user's name)
noReplyBooleantrue if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for Google Apps accounts, not Gmail users
replyToStringan email address to use as the default reply-to address (default: the user's email address)
subjectStringthe subject of the email
toStringthe address of the recipient

See also


sendEmail(recipient, subject, body)

Sends an email message.

 
MailApp.sendEmail("recipient@example.com",
                   "TPS reports",
                   "Where are the TPS reports?");
 

Parameters

NameTypeDescription
recipientStringthe addresses of the recipients, separated by commas
subjectStringthe subject line
bodyStringthe body of the email

sendEmail(recipient, subject, body, options)

Sends an email message with optional arguments.

 
// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
 var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
 var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
 MailApp.sendEmail('mike@example.com', 'Attachment example', 'Two files are attached.', {
     name: 'Automatic Emailer Script',
     attachments: [file.getAs(MimeType.PDF), blob]
 });
 

Parameters

NameTypeDescription
recipientStringthe addresses of the recipients, separated by commas
subjectStringthe subject line
bodyStringthe body of the email
optionsObjecta JavaScript object that specifies advanced parameters, as listed below

Advanced parameters

NameTypeDescription
attachmentsBlobSource[]an array of files to send with the email (see example)
bccStringa comma-separated list of email addresses to BCC
ccStringa comma-separated list of email addresses to CC
htmlBodyStringif set, devices capable of rendering HTML will use it instead of the required body argument; you can add an optional inlineImages field in HTML body if you have inlined images for your email
inlineImagesObjecta JavaScript object containing a mapping from image key (String) to image data (BlobSource); this assumes that the htmlBody parameter is used and contains references to these images in the format <img src="cid:imageKey" />
nameStringthe name of the sender of the email (default: the user's name)
noReplyBooleantrue if the email should be sent from a generic no-reply email address to discourage recipients from responding to emails; this option is only possible for Google Apps accounts, not Gmail users
replyToStringan email address to use as the default reply-to address (default: the user's email address)

See also


sendEmail(to, replyTo, subject, body)

Sends an email message. This method allows a user to easily specify a Reply-To address for the sent message that can differ from the sender.

 
MailApp.sendEmail("recipient@example.com",
                   "replies@example.com",
                   "TPS report status",
                   "What is the status of those TPS reports?");
 

Parameters

NameTypeDescription
toStringthe addresses of the recipients, separated by commas
replyToStringthe reply-to address
subjectStringthe subject line
bodyStringthe body of the email in plain text

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.