Application Integration

Using the SendGrid API to send emails from your mobile apps

blog product titanium

SendGrid is a cloud-based service that exposes and easy-to-use API to send emails from your apps, and Appcelerator apps are no exception. Sendgrid just published an Appcelerator library that makes it even easier to use.

To use it, first sign up for a free SendGrid account at https://sendgrid.com. The user name and password you choose will be the ones you’ll use for the library.

The library is currently hosted on GitHub and soon will be available in the Appcelerator Marketplace. Locate the file tisendgrid.js and copy it to your project folder.

After doing this, using the library is as easy as obtaining a reference and calling the send method.

var sendgrid = require('tisendgrid')(sendgrid_username, sendgrid_password);
var email = {
    to:         email_to_address,
    from:       email_from_address,
    subject:    email_subject,
    text:       email_message_text
};
var message = sendgrid.Email(email);
sendgrid.send(message, function(e) {
    if (e) {
        console.log(JSON.stringify(e));
        alert(e.errors[0]);
    }else{
        alert('Message sent');
    }
});

This code is all you need to send a simple email. If you’d like to send to multiple recipients, simply assign an array of email addresses to the to field like so:

to: ['address1@email.com','address2@email.com']

The library also has some other advanced functionalities such as filters, explained in their own blog post.

That’s all there is to it. Stay tuned for a future blog post where I’ll show you how to use SendGrid from your Node.ACS apps.