Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Composing emails in a NativeScript app

    Brad MartinBrad Martin

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

    nativescriptNativeScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 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.

    00:23 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.

    00:41 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.

    01:08 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.

    01:32 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."

    01:55 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.

    02:34 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.

    02:59 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.

    03:08 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.

    Discuss

    Discuss