Wall's Corners Wall's Corners Author
Title: Send Emails In An Ionic 2 App Via The Rackspace Mailgun API
Author: Wall's Corners
Rating 5 of 5 Des:
Not too long ago I wrote about sending emails in an Ionic Framework app using the Rackspace Mailgun API .  To get you up to speed, I often g...

Not too long ago I wrote about sending emails in an Ionic Framework app using the Rackspace Mailgun API.  To get you up to speed, I often get a lot of questions regarding how to send emails without opening the default mail application from within an Ionic Framework application.  There are a few things that could be done.  You can either spin up your own API server and send emails from your server via an HTTP request or you can make use of a service.

To compliment the previous post I wrote for Ionic Framework, I figured it would be a good idea to demonstrate how to use Mailgun in an Ionic 2 application.

Mailgun, for the most part, is free.  It will take a lot of emails before you enter the paid tier.  Regardless, you’ll need an account to follow along with this tutorial.

Let’s start by creating a new Ionic 2 project.  From the Command Prompt (Windows) or Terminal (Mac and Linux), execute the following:

A few important things to note here.  The first thing to note is the use of the --v2 tag.  This is an Ionic 2 project, and to create Ionic 2 projects, the appropriate Ionic CLI must be installed.  Second, notice the --typescript tag.  This is going to be a TypeScript project rather than JavaScript.  Finally, you must be using a Mac if you wish to add and build for the iOS platform.

This project uses no plugins or external dependencies.  This means we can start coding our application.

Coding the Mailgun Logic

Starting with the logic file, open your project’s app/pages/home/home.ts file and include the following code:

There is a lot going on in the above snippet so let’s break it down.

First you’ll notice the following imports:

The Mailgun API is accessed via HTTP requests.  To make HTTP requests we must include the appropriate Angular 2 dependencies.  More on HTTP requests can be seen in a previous post I wrote specifically on the topic.

Next we define our Mailgun domain URL and corresponding API key.  For prototyping I usually just use the sandbox domain and key, but it doesn’t cost any extra to use your own domain.  However, notice the following:

We are making use of the btoa function that will create a base64 encoded string.  Having a base64 encoded string is a requirement when using the authorization header of a request.

Finally we get into the heavy lifting.  The send function is where all the magic happens.

The send function expects an authorization header as well as a content type.  The Mailgun API expects form data to be sent, not JSON data.  This data is to be sent via a POST request.

Creating the Simple UI

This brings us into our UI.  To keep things simple our UI is just going to be a form that submits to the sendfunction.  Open your project’s app/pages/home/home.html file and include the following code:

The above form has two standard text input fields for the recipient and subject.  There is a text area for the message body and a button for submitting the form data.  The input elements are all bound using an ngModel.  This allows us to pass the fields into the send function.

Give it a try, you should be able to send emails now via HTTP.

Share This:

View more at: http://bit.ly/1XReQFr

About Author

Advertisement

Post a Comment

 
Top