Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending mail in ionic app

I have a simple page with 3 textbox 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 value in first text-field as subject and other two as body in angular js/ionic.

like image 567
varun aaruru Avatar asked Dec 21 '15 13:12

varun aaruru


1 Answers

So, basically the comments to your question are correct - you can't do this purely in JavaScript, you need to use a backend service for it.

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

The call would, for example look like this:

$http({
    method: 'POST',
    url: 'http://your.server.com/sendmail.php',
    data: { mailTo: '[email protected]', 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 the email) located
  • data - what are you sending to this service endpoint

In my example I've put the service url to be sendmail.php 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're familiar with (if you'll research this topic further make sure you read about the RESTful services).

For the sake of this example, the PHP script (unsecured and just for reference) which uses the mail function to send an email would look something like this:

<?php

$to = $_POST["emailTo"];
$msg = $_POST["msg"];

mail($to, "Some test subject", $msg);

?>

Hope this helps clear the confusion.

like image 140
Nikola Avatar answered Nov 17 '22 21:11

Nikola