AngularJS mailto not sending emails

All we need is an easy explanation of the problem, so here it is.

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;
     };

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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

Method 2

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)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply