3

I've been searching high and low for an answer to this question. I've downloaded and successfully installed the EmailComposer plugin for PhoneGap. I'm able to click on my "Email" button the email field pops up no problem there. The issue is, i'm trying to pre-populate the "To:" field and the "Subject" field. I've searched various posts (mainly on StackOverflow) and i've seen some similar posts, but nothing with this exact same issue or a real solution:

posts I've tried: PhoneGap Email Composer Plugin How to send email using mail composer plugin in iphone using phone gap jQuery mobile

The closest I was able to find has been here: https://groups.google.com/forum/#!searchin/phonegap/EmailComposer/phonegap/ItUj30UZnig/XTaBK7B8ahsJ

But i haven't had any luck with the solution there.

As mentioned above. All i'm trying to do is pre-populate the "To:" and "Subject:" field. The included EmailComposer.js doc looks like this:

// window.plugins.emailComposer

function EmailComposer() {
this.resultCallback = null; // Function
}

EmailComposer.ComposeResultType = {
    Cancelled:0,
    Saved:1,
    Sent:2,
    Failed:3,
    NotSent:4
}


// showEmailComposer : all args optional
EmailComposer.prototype.showEmailComposer = function(subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML) {

    var args = {};

    if(toRecipients)
        args.toRecipients = toRecipients;
    if(ccRecipients)
        args.ccRecipients = ccRecipients;
    if(bccRecipients)
        args.bccRecipients = bccRecipients;
    if(subject)
        args.subject = subject;
    if(body)
        args.body = body;
    if(bIsHTML)
        args.bIsHTML = bIsHTML;

    cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]);

}

// this will be forever known as the orch-func -jm
EmailComposer.prototype.showEmailComposerWithCB =     function(cbFunction,subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML) {
    alert("email showEmailComposerWithCB?");
    this.resultCallback = cbFunction;
    this.showEmailComposer.apply(this,    [subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML]);
}

EmailComposer.prototype._didFinishWithResult = function(res) {
    this.resultCallback(res);
}



cordova.addConstructor(
    function()  {
        if(!window.plugins) {
            window.plugins = {};
        }

        // shim to work in 1.5 and 1.6
        if (!window.Cordova) {
            window.Cordova = cordova;
        };

        //window.plugins.emailComposer.showEmailComposer(subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML)
        window.plugins.emailComposer = new     EmailComposer();
    }
);

The actual line that starts with:

EmailComposer.prototype.showEmailComposer

Is never actually fired. On the public facing side, I have this (it's valid HTML but i removed some tags for posting purposes:

<a onclick="cordova.exec(null, null, 'EmailComposer', 'showEmailComposer', [args]);">Send Email</a>

I then have:

app.initialize();
var args;

Which happens when the page first loads.

Any ideas on what I'm doing wrong here?

Community
  • 1
  • 1
doubleya
  • 479
  • 9
  • 17

1 Answers1

0

(Answered by the OP in a question edit. Moved to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

The OP wrote:

I figured it out! The solution was relatively easy, but for the life of me, I couldn't get it for the first couple of hours.

In your HTML code, wherever your button is, put this:

<a onclick="sendEmail(); return false;">Send Email Now</a>

that sendEmail() function is where the business is, so on the same page, create a script tag and add the following:

<script>
    function sendEmail(){
         window.plugins.emailComposer.showEmailComposer("subject","body", "recipient@something.com", "cc@something.com", "bcc@something.com",false); 
    }
</script>

In the same HTML after your app has been initialized, just add the args variable:

app.initialize(); //under this
var args = {};

Keep the EmailComposer.js file the same and all should be well. In search of this answer, I ran across a ton of posts where people could pass the subject line, but couldn't pass the body or anything else. I tested it out, and this will pass all field properly. I hope this helps someone.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129