Sending mail in the ionic application

I have a simple page with 3 text fields and a button like this.

<input type="text" ng-model="name" required > <input type="text" ng-model="city" required > <input type="text" ng-model="country" required > <button class="button button-block button-royal ExploreBtn" ng-click="sendMail();"> Send mail </button> 

Help me how to send mail with a value in the first text field as a subject, and the other two as a body in angular js / ionic.

+6
source share
1 answer

So, basically the comments on your question are correct - you cannot do this exclusively in JavaScript, you need to use a backend service for this.

Now, what you would do in your sendMail function is called by the service using the $http angles service. You can learn more about the $ http service from the official documentation .

The call will look, for example, as follows:

 $http({ method: 'POST', url: 'http://your.server.com/sendmail.php', data: { mailTo: ' me@gmail.com ', msg: 'hello!' } }).then(function successCallback(response) { alert("msg sent!"); }, function errorCallback(response) { alert("error with sending a msg"); }); 

Here you have two important parts:

  • url - where is your service (which will finally send an email) located
  • data is what you send to this service endpoint.

In my example, I put the URL of the sendmail.php service, which will eventually be written in PHP. I must emphasize that your server service can be written in any server language that you are familiar with (if you will study this topic, then make sure that you read about RESTful services ).

For this example, a PHP script (unsecured and for reference only) that uses the mail function to send email will look something like this:

 <?php $to = $_POST["emailTo"]; $msg = $_POST["msg"]; mail($to, "Some test subject", $msg); ?> 

Hope this helps resolve the confusion.

+1
source

All Articles