{"id":2449,"date":"2015-12-22T13:06:56","date_gmt":"2015-12-22T13:06:56","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2449"},"modified":"2015-12-22T13:10:59","modified_gmt":"2015-12-22T13:10:59","slug":"how-to-send-an-email-in-ionic-framework-application","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/ionic\/how-to-send-an-email-in-ionic-framework-application\/","title":{"rendered":"How to send an email in Ionic framework application?"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[2449]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts, I will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>As you may know, I&#8217;m really into Ionic framework lately\u00a0and am helping out on StackOverflow with the knowledge I gained so far with the framework. I&#8217;m in the <a href=\"http:\/\/stackoverflow.com\/tags\/ionic\/topusers\">top All time answerers<\/a> list.<\/p>\n<p>I answered <a href=\"http:\/\/stackoverflow.com\/questions\/34396437\/sending-mail-in-ionic-app\/34397520\">this question<\/a> by user\u00a0varun:<\/p>\n<blockquote><p>I have a simple page with 3 textbox and a button like this.<\/p>\n<pre class=\"lang:default decode:true\">&lt;input type=\"text\" ng-model=\"name\" required &gt;\r\n&lt;input type=\"text\" ng-model=\"city\" required &gt;\r\n&lt;input type=\"text\" ng-model=\"country\" required &gt;\r\n\r\n&lt;button class=\"button button-block button-royal ExploreBtn\" ng-click=\"sendMail();\"&gt;\r\nSend mail               \r\n&lt;\/button&gt;<\/pre>\n<p>Help me , How to send mail with value in first text-field as subject and other two as body in angular js\/ionic.<\/p><\/blockquote>\n<p><a href=\"http:\/\/stackoverflow.com\/a\/34397520\/534755\">My answer<\/a> was rather elaborate:<\/p>\n<blockquote><p>So, basically the comments to your question are correct &#8211; you can&#8217;t do this purely in JavaScript, you need to use a backend service for it.<\/p>\n<p>Now, what you would do in your <code>sendMail<\/code> function is call the service by using Angulars&#8217; <code>$http<\/code>service. You can learn more about $http service from the <a href=\"https:\/\/docs.angularjs.org\/api\/ng\/service\/$http\" rel=\"nofollow\">official documentation<\/a>.<\/p>\n<p>The call would, for example look like this:<\/p>\n<pre class=\"lang:default decode:true\">$http({\r\n    method: 'POST',\r\n    url: 'http:\/\/your.server.com\/sendmail.php',\r\n    data: { mailTo: 'me@gmail.com', msg: 'hello!' }\r\n}).then(function successCallback(response) {\r\n    alert(\"msg sent!\");\r\n}, function errorCallback(response) {\r\n    alert(\"error with sending a msg\");\r\n});<\/pre>\n<p>Here you have two important parts:<\/p>\n<ul>\n<li><code>url<\/code> &#8211; where is your service (which will finally send the email) located<\/li>\n<li><code>data<\/code> &#8211; what are you sending to this service endpoint<\/li>\n<\/ul>\n<p>In my example I&#8217;ve put the service url to be <code>sendmail.php<\/code> which, in end, would be written in PHP. I have to stress out that your backend service can be written in any server-side language you&#8217;re familiar with (if you&#8217;ll research this topic further make sure you read about the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer\" rel=\"nofollow\">RESTful services<\/a>).<\/p>\n<p>For the sake of this example, the PHP script (<em>unsecured and just for reference<\/em>) which uses the <a href=\"http:\/\/php.net\/manual\/en\/function.mail.php\" rel=\"nofollow\">mail function<\/a> to send an email would look something like this:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n$to = $_POST[\"emailTo\"];\r\n$msg = $_POST[\"msg\"];\r\n\r\nmail($to, \"Some test subject\", $msg);\r\n\r\n?&gt;<\/pre>\n<p>Hope this helps clear the confusion.<\/p>\n<p>https:\/\/twitter.com\/HitmanHR\/status\/679287774650998785<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts, I will be posting my&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1991,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-2449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2449"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2449\/revisions"}],"predecessor-version":[{"id":2452,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2449\/revisions\/2452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1991"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}