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:

public class EmailEnvelope {
   private String query;
   private Set<String> to;
   private Set<String> cc;
   private Set<String> bcc;

   // adds an email address to what's already in the envelope
   public void addTo( String email );

   // adds a collection of addresses to what's already in the envelope
   public void addTo( Set<String> emails );

   // removes the existing "to" collection and sets the provided one
   public void setTo( Set<String> emails );

   public void addCc( String email );
   public void addCc( Set<String> emails );
   public void setCc( Set<String> emails );

   public void addBcc( String email );
   public void addBcc( Set<String> emails );
   public void setBcc( Set<String> emails );
}

where:

Argument                Description
to 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.
cc An array of email addresses to include into the CC (carbon copy) distribution list of the email message generated from the template.
bcc 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 to property.

Non-blocking methods

public void sendEmailFromTemplate( String templateName, 
                                   EmailEnvelope envelope, 
                                   AsyncCallback<MessageStatus> responder )

public void sendEmailFromTemplate( String templateName, 
                                   EmailEnvelope envelope, 
                                   Map<String, String> templateValues, 
                                   AsyncCallback<MessageStatus> responder )
public void sendEmailFromTemplate( String templateName, 
                                  EmailEnvelope envelope, 
                                  AsyncCallback<MessageStatus> responder )

public void sendEmailFromTemplate( String templateName, 
                                   EmailEnvelope envelope, 
                                   Map<String, String> templateValues, 
                                   AsyncCallback<MessageStatus> responder )

Blocking methods

public sendEmailFromTemplate( String templateName, 
                              EmailEnvelope envelope )

public sendEmailFromTemplate( String templateName, 
                              EmailEnvelope envelope, 
                              Map<String, String> templateValues )
public sendEmailFromTemplate( String templateName, 
                              EmailEnvelope envelope )

public sendEmailFromTemplate( String templateName, 
                              EmailEnvelope envelope, 
                              Map<String, String> templateValues )

where:

Argument                Description
templateName Name of an email template created in Backendless Console.
envelope An instance of the EmailEnvelope class containing information about the email recipients.
templateValues An object containing values which will be used Smart and Dynamic text substitutions. The key names in the object are matched against the Smart/Dynamic text placeholder names. The corresponding values are used for substitution in the resulting email message.

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:

EmailEnvelope envelope = new EmailEnvelope();
envelope.setQuery("email not like '%@gmail.com' and address.country = 'USA'");

Backendless.Messaging.sendEmailFromTemplate("Marketing Template", envelope, new AsyncCallback<MessageStatus>() {
   @Override
   public void handleResponse(MessageStatus response) {
       Log.i(TAG, "Email has been sent");
   }

   @Override
   public void handleFault(BackendlessFault fault) {
       Log.e(TAG, fault.getMessage());
   }
});
val envelope = EmailEnvelope()
envelope.query = "email not like '%@gmail.com' and address.country = 'USA'"

Backendless.Messaging.sendEmailFromTemplate("Marketing Template", envelope,
   object : AsyncCallback<MessageStatus> {
       override fun handleResponse(response: MessageStatus) {
           Log.i(TAG, "Email has been sent")
       }

       override fun handleFault(fault: BackendlessFault) {
           Log.e(TAG, fault.message)
       }
   })

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:

Set<String> addresses = new HashSet<>();
addresses.add("joe@yahoo.com");
addresses.add("bob@hotmail.com");

Map<String, String> templateValues = new HashMap<>();
templateValues.put("Users.address.country", "your country");
templateValues.put("discount", "20% off");

EmailEnvelope envelope = new EmailEnvelope();
envelope.setTo(addresses);

Backendless.Messaging.sendEmailFromTemplate("Marketing Template", envelope, templateValues, new AsyncCallback<MessageStatus>() {
   @Override
   public void handleResponse(MessageStatus response) {
       Log.i(TAG, "Email has been sent");
   }

   @Override
   public void handleFault(BackendlessFault fault) {
       Log.e(TAG, fault.getMessage());
   }
});
val addresses = setOf("joe@yahoo.com", "bob@hotmail.com")

val templateValues = mapOf("Users.address.country" to "your country", "discount" to "20% off")

val envelope = EmailEnvelope()
envelope.to = addresses

Backendless.Messaging.sendEmailFromTemplate("Marketing Template", envelope, templateValues,
   object : AsyncCallback<MessageStatus> {
       override fun handleResponse(response: MessageStatus) {
           Log.i(TAG, "Email has been sent")
       }

       override fun handleFault(fault: BackendlessFault) {
           Log.e(TAG, fault.message)
       }
   })