Skip to content

Send Emails with Templates API

The API for sending email for a template relies on a special entity called EmailEnvelope. This entity contains information about the email recipients. Specifically:

  • a list of email addresses for the "To", "Cc" and "Bcc" fields of the email message
  • a query, also known as a where clause, against the Users database table, which identifies the recipients of the email. The query takes precedence and overrides the email addresses for the "To" field.

The EmailEnvelope class is defined as:

class EmailEnvelope {
  /**
   * @param {Array|String} toAddresses
   * @returns {EmailEnvelope}
   */
  setTo( toAddresses );

  /**
   * @param {Array|String} toAddresses
   * @returns {EmailEnvelope}
   */
  addTo( toAddresses );

  /**
   * @returns {Array} - toAddresses
   */
  getTo();

  /**
   * @param {Array|String} ccAddresses
   * @returns {EmailEnvelope}
   */
  setCc( ccAddresses );

  /**
   * @param {Array|String} ccAddresses
   * @returns {EmailEnvelope}
   */
  addCc( ccAddresses );

  /**
   * @returns {Array} - ccAddresses
   */
  getCc();

  /**
   * @param {Array|String} bccAddresses
   * @returns {EmailEnvelope}
   */
  setBcc( bccAddresses );

  /**
   * @param {Array|String} bccAddresses
   * @returns {EmailEnvelope}
   */
  addBcc( bccAddresses );

  /**
   * @returns {Array} - bccAddresses
   */
  getBcc();

  /**
   * @param {String|null} query
   * @returns {EmailEnvelope}
   */
  setQuery( query );

  /**
   * @returns {String} - query
   */
  getQuery()

where:

Argument                Description
toAddresses An array of email addresses to deliver an email generated from the template to. These email addresses are ignored if and when the query parameter is present.
ccAddresses An array of email addresses to include into the CC (carbon copy) distribution list of the email message generated from the template.
bccAddresses An array of email addresses to include into the BCC (blind carbon copy) distribution list of the email message generated from the template.
query A where clause for the Users table which defined the condition for selecting the users who will be receiving an email message generated from the template. The resulting collection of users takes precedence of the the email addresses (if any are) provided through the toAddresses property.

Example

The example below sends out an email based on a template called "Marketing Template" to all users registered in the app whose email address does not end with @gmail.com and the related country is USA:

const templateName = 'Marketing Template'
const emailEnvelope = new Backendless.Messaging.EmailEnvelope()

emailEnvelope.setQuery( "email not like '%@gmail.com and address.country = 'USA'" )

Backendless.Messaging.sendEmailFromTemplate( templateName, emailEnvelope )
  .then(result => console.log(result))
  .catch(error => console.log(error))

The example below sends out an email based on a template called "Marketing Template" to the users specified in the "to" field of EmailEnvelope. The example demonstrates the usage of the "templateValues" field. It contains two substitution values for the "Users.address.country" and the "discount" placeholders:

const templateName = 'Marketing Template'

const emailEnvelope = new Backendless.Messaging.EmailEnvelope()
emailEnvelope.setTo(['joe@yahoo.com', 'bob@hotmail.com'])

const templateValues = {
  'Users.address.country': 'your country',
  discount: '20% off'
}

Backendless.Messaging.sendEmailFromTemplate( templateName, emailEnvelope, templateValues )
  .then(result => console.log(result))
  .catch(error => console.log(error))