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) locateddata 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.
source share