Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 986 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Composing emails in a NativeScript app

3:19 JavaScript lesson by

We will use the NativeScript-Email plugin from Eddy Verbruggen to compose email messages from our app that the user can send out.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

We will use the NativeScript-Email plugin from Eddy Verbruggen to compose email messages from our app that the user can send out.

We'll start by adding the plugin using the tns plugin add command. The name of the plugin is nativescript-email. Now that we have the plugin installed we'll just add a stack-layout to the page. Inside of stack-layout we're just going to add a button to fire off the event with the plugin.

We use the tap event on the button to execute our function. We'll name of the function openEmail. Now that we have a button on the main-page.xml for the interface, we'll move to the main-page.ts, for the code behind for the interface.

First thing is we'll import the entire email plugin. Next we need to define the function that was set on the tap event of the button, we named it openEmail. The first thing you want to do before sending an email is check the device and make sure it's capable of sending email.

The plugin actually provides the available method, which we can call, and it returns a promise and the result is a Boolean. If this result is true, we know we can send an email on this device.

Now that we know we can send an email, we can use the compose method from the plugin, which is expecting an object type of the compose options which is provided by the plugin as an interface. First property we'll set as the subject, just say test. The body, "Hello, egghead."

The to property accepts an array of strings, we're just going to send this to myself. There's also BCC and CC if you want to carbon copy, but we'll just keep it simple here. This also returns a promise. We'll just log the result here.

Now when we tap the button we're going to execute the openEmail function, which first we're going to check if the email capability is possible on this device. If the result returns true, then we call the compose method which should open up a dialog to open up the email application on the device, and should pre-fill the email subject, the body, and the to property.

We'll go ahead and tap the button, and we're presented with the Android action sheet. We're going to go ahead and select the Inbox application.

There you see the to address is filled, the subject, and the body. You can also send attachments in the options object for the compose method.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?