Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS mailto not sending emails

Tags:

angularjs

I am using the following code to allow my AngularJS app users to send emails using their favorite mail client, but when I click on the Send button nothing happens. Can someone please check my code and tell me what exactly I am missing here? Thanks

<button type="button" ng-click="sendEmail(message.Email, message.subject, message.body)" >Send</button>

Controller code:

    $scope.sendEmail = function(email, subject, body){
        var link = "mailto:"+ email
                 + "&subject=New email " + escape(subject);
                 + "&body=" + escape(body); 

        window.location.href = link;
     };
like image 822
MChan Avatar asked Apr 08 '14 00:04

MChan


2 Answers

2 things I can think may be wrong here.

First off, Spaces aren't valid characters in the subject. You may have to replace this with a %20.

Second, you will need to change the & before the subject to a ?. Otherwise it will try to send an email to the address including all of the subject and body parameters...

Can you try this instead:

$scope.sendEmail = function(email, subject, body) {
    var link = "mailto:"+ email
             + "?subject=New%20email " + escape(subject)
             + "&body=" + escape(body); 

    window.location.href = link;
 };

You can view some more info here: http://en.wikipedia.org/wiki/Mailto

like image 163
FamiliarPie Avatar answered Oct 12 '22 23:10

FamiliarPie


If someone is still using the upvoted answer, remember to remove the semi-colon on the subject escape.

this:

+ escape(subject);

should be:

+ escape(subject)
like image 35
Nine Magics Avatar answered Oct 13 '22 00:10

Nine Magics