在离子应用程序中发送邮件

var*_*uru 6 javascript angularjs cordova ionic-framework

我有一个简单的页面,包含3个文本框和一个这样的按钮.

<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>
Run Code Online (Sandbox Code Playgroud)

帮助我,如何在第一个文本字段中将有价值的邮件作为主题发送,将另外两个邮件作为角度js/ionic中的正文发送.

Nik*_*ola 1

所以,基本上对你的问题的评论是正确的 - 你不能纯粹用 JavaScript 来做到这一点,你需要使用后端服务。

现在,您在函数中要做的sendMail就是使用 Angulars 的 $http服务来调用该服务。您可以从官方文档了解更多关于$http服务的信息。

例如,该调用如下所示:

$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");
});
Run Code Online (Sandbox Code Playgroud)

这里有两个重要的部分:

  • url- 您的服务(最终将发送电子邮件)位于哪里
  • data- 您要向此服务端点发送什么

在我的示例中,我将服务 URL 设置为sendmail.php最终用 PHP 编写的服务 URL。我必须强调,您的后端服务可以用您熟悉的任何服务器端语言编写(如果您要进一步研究此主题,请确保您阅读有关 RESTful 服务的信息

对于此示例,使用邮件函数发送电子邮件的 PHP 脚本(不安全且仅供参考)将如下所示:

<?php

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

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

?>
Run Code Online (Sandbox Code Playgroud)

希望这有助于消除困惑。