Send Emails Directly from the Browser

Our JavaScript client has landed, and we’re happy to announce that it works both on the server and the client. Previously sending emails has always been the domain of the back-end, but today we’re changing that.

How it works

The JavaScript API works just like the other Apostle.io client libraries. You first configure it to use your domain key, and then you can easily send your emails with the deliver method. It’s incredibly simple to get up and sending.

1
2
3
apostle.domainKey = "your domain key"; apostle.deliver("welcome_email", { email: "mal@apostle.io" })

To add error handling, just add success and error callbacks (deliver returns a promise object).

1
2
3
4
5
6
7
apostle.deliver("welcome_email", { email: "mal@apostle.io" }) .then( // Success Callback function(){ console.log("Welcome email sent!"); }, // Error Callback function(message, response){ console.error(message); } )

For more examples, including queues and error types, check out the GitHub repo.

Is that a good idea?

I’m sure there are some people who won’t like the idea of sending directly from the browser, and there are also security concerns to be aware of. By placing your domain key in your public source code, you’re effectively allowing anybody to be able to send email on your behalf. Of course, as with all of our client libraries, they will only be able to send your templates.

We take security seriously, and will be looking at ways to reduce that attack footprint that is opened by having credentials in the client. One approach is to generate a token on the server side that can only be used for a period of time, however one of the primary use cases we see is for adding dynamic functionality to apps that don’t have a back end, and this would not work in that case.

Great, how do I get started?

To get started, you’ll need to sign up as an Apostle.io customer (it’s free), then all you need to do is get your hands on the Apostle.js library and you’re good to go.

About the author

Mal Curtis is the Co-Founder of Apostle.io (an easier way to manage transactional emails). He’s been building web apps for over a decade and loves new technology.

Follow him on Twitter
comments powered by Disqus