20

I'm implementing a simple email feedback feature in angular app. The mail has predefined mail subject and content template. The angular controller need bring up client email client (like invoke "mailto:foo@bar.com") and fulfill predefined subject, content template. Any body know how to implement it?

Makoto
  • 104,088
  • 27
  • 192
  • 230
duckegg
  • 1,379
  • 2
  • 13
  • 20
  • 2
    possible duplicate of [Sending emails with Javascript](http://stackoverflow.com/questions/271171/sending-emails-with-javascript) – artur grzesiak Dec 03 '14 at 21:37
  • 2
    You could probably wrap it into a directive. [Here's one someone else put together](https://github.com/updatezen/angular-mailto). – Eric McCormick Mar 20 '15 at 15:15
  • 3
    If you now have an answer to this question, you should enter the answer below (not as part of the question) and mark it as the answer, so it is immediately clear to other StackOverflow users this question has already been solved. – Jason Parker Apr 27 '15 at 15:14

4 Answers4

18

Inject $window and use $window.open() method.

Inside controller define...

$scope.sendMail = function(emailId,subject,message){
    $window.open("mailto:"+ emailId + "?subject=" + subject+"&body="+message,"_self");
};

and call it like...

$scope.sendMail("foo@bar.com","Mail Subject","Mail Body Message");
Ankit Pundhir
  • 1,097
  • 8
  • 13
5

use $window.location:

$window.location = "mailto:..."
zhekaus
  • 3,126
  • 6
  • 23
  • 46
  • this doesn't open not needed new browser tab, while location.open() does. Seems better if you are sure that user need a external mail client to be opened. – garfunkel61 Dec 01 '16 at 11:51
2

This should open new tab for Google mail or email client, depending on users settings.

In Angular JS: Concatenate string in controller like so:

$scope.mailLink = "mailto:" + $scope.emailId + "?subject=" + $scope.Subject + '&body=' + $scope.bodyText;

html

<a ng-href="{{mailLink}}" target="_blank">Send</a>
Uliana Pavelko
  • 2,824
  • 28
  • 32
1

location.href works too!

$scope.email = function(item){
    location.href= 'mailto:' + $scope.allemails (array) + '?subject=Subject you want';
}

Note: If you have an array in $scope.allemails, and you will use method .join(', ') - thunderbringer email client will not recognize this as a collection of emails and it will add a new line of 'To:' to every email from that array.

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26