We will use the NativeScript-Email plugin from Eddy Verbruggen to compose email messages from our app that the user can send out.
[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.