1

I tried to use webintent http://smus.com/android-phonegap-plugins to send email in phonegap android 4 application. But I don't really understand the website nor the plugins readme file.

e.g. how to use this?

window.plugins.webintent.startActivity({
    action: WebIntent.ACTION_VIEW,
    url: 'geo:0,0?q=' + address}, 
    function() {}, 
    function() {alert('Failed to open URL via Android Intent')};
);

<a href="mailto:support@fareastgadget.com&subject=Report%20issues&body=Reporting%20following%20issues:">

If I use html markup, the android phone will just filter the url and cause email recipient to be the whole string.

Can anyone provide some sample codes or tutorial on how to sending email in phonegap (not necessary webintent though)?

Thanks.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
Kiddo
  • 5,052
  • 6
  • 47
  • 69

2 Answers2

1

Used the markup email solution and the client came back saying that the emails were blank, not adding the content on certain devices. So I set out to implement the web intent plugin (as you can see from the package name) but I ended up just implementing a new email plugin.

Here is the Java class.

package com.webintent.emailcomposer;

import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import com.phonegap.api.Plugin;

public class EmailComposer extends Plugin {
    public final String ACTION_SEND_EMAIL = "SendEmail";

    public PluginResult execute(String action, JSONArray arg1, String callbackId) {

    PluginResult result = new PluginResult(Status.INVALID_ACTION);
    if (action.equals(ACTION_SEND_EMAIL)) {
        try {
            String email = arg1.getString(0);
            String subject = arg1.getString(1);
            String message = arg1.getString(2);
            this.sendEmail(email, subject, message);
            result = new PluginResult(Status.OK);
        }
        catch (JSONException ex) {
            result = new PluginResult(Status.JSON_EXCEPTION, ex.getMessage());
        }           
    }
    return result;
}

private void sendEmail(String email, String subject, String message) {
    final Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});
    intent.putExtra(
            Intent.EXTRA_TEXT, 
            Html.fromHtml(new StringBuilder()
                .append(message)
                .toString())
            );
    intent.setType("text/html");
    this.ctx.startActivity(Intent.createChooser(intent, "Choose email account"));
}
}

Remember to update plugins.xml

Here is the js plugin code

var EmailPlugin = function () {};

cordova.addConstructor(function() {
   return cordova.addPlugin("email", new EmailPlugin());
});

EmailPlugin.prototype.send = function (email, subject, message){
cordova.exec(function(){ alert('email success')}, 
    function(){ alert('email fail')}, 
    'EmailComposer', 
    'SendEmail', 
    [email, subject, message]);
}

And finally, you call the plugin as such.

window.plugins.email.send(email, subject, body);

Note I didn't include callbacks in the parameters, but you can see where they would go.

user1496391
  • 113
  • 1
  • 8
0

Well, that looks like the code Boris Smus provides for using google maps. The Email example is this.

Android.sendEmail = function(subject, body) { 
var extras = {};
extras[WebIntent.EXTRA_SUBJECT] = subject;
extras[WebIntent.EXTRA_TEXT] = body;
window.plugins.webintent.startActivity({ 
  action: WebIntent.ACTION_SEND,
  type: 'text/plain', 
  extras: extras 
}, 
function() {}, 
function() {
  alert('Failed to send email via Android Intent');
}
); 
};

I'm having trouble with it myself as for me, it brings up the MMS composer instead of email...

*Edit: Thought I had something, but no... Other sources seem to suggest using a mailto link.

*Edit 2: Ignore this and see the other answer.

user1496391
  • 113
  • 1
  • 8
  • yeah, i solved this by using a mailto link, it does the work for now – Kiddo Aug 03 '12 at 10:45
  • *Sigh Yes, I couldn't get it to work either. Modified the suggestion here. http://stackoverflow.com/questions/5265222/mailto-from-javascript – user1496391 Aug 03 '12 at 14:29