Use Cordova to Initiate an Email
Since your Cordova app runs in an HTML Layout Engine, there’s a very straight-forward way to initiate an email: a link.
However, if you want to create a nicely formatted email, or a longer one, you need to use a plugin to invoke the native interface. I went with katzer’s cordova-plugin-email-composer, and it works well.
My purpose was to simply create a blank email draft, with only title and content.
The following code wrapper encapsulates the plugin call, taking into account:
- we’re running on a device with email not installed
- we’re running on a non-mobile device (as I often do when debugging the app)
function draftEmail(subject, message) { if (!window.plugin){ //non-mobile - plugins are not present. alert("Email plugin is not available"); return; } if (!isAvailable){ //mobile, but no email installed alert("Email is not available") return; } window.plugin.email.open({ to: [], cc: [], bcc: [], attachments: [], subject: subject, body: message, isHtml: true }); } //runs on application startup, to check for installed email var isAvailable = false; if (window.plugin){ window.plugin.email.isServiceAvailable( function (emailInstalled) { isAvailable = emailInstalled; } ); }
It would be more elegant to hide the “Share” button if email is not installed on the device, but for demo purposes this works.
One comment