我正在使用 AngularJS 和 PHPMailer 构建一个应用程序。我需要将一个数组从我的 Angular 应用程序传递给 PHPMailer,然后在 PHPMailer 中检索该数组并将数据邮寄到客户端。
$scope.data = {};
var link = 'http://uvw.com/mail.php';
$http.post(link, {order:$scope.listItems}).then(function (res){
$scope.response = res.data;
console.log($scope.response);
});
这里,$scope.listItems 是一个数组,它由客户名称、客户电子邮件、客户电话等头部字段以及每个头部下的大量数据组成。所以它归结为 $scope.listItems.customerName、$scope.listItems.customerPhone 等。我使用 angular forEach 很容易地检索 Angular 中的数据。
我已成功将数据传递给 PHPMailer,但我不知道如何在 PHPMailer 中检索数据并将其转发。
更新
PHPMailer 代码
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers:
{$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$postdata = file_get_contents("php://input");
if (isset($postdata)) {
$request = json_decode($postdata);
foreach ($order as $value) {
$body=$order;
}
require("PHPMailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = "support@uvw.com";
$mail->Password = "support@123";
$mail->From = "support@uvw.com";
$mail->FromName = "uvw";
$mail->AddAddress("orders@uvw.com", "Orders");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "New Customer";
$mail->Body = $body;
$mail->AltBody = "Alternate Body";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
else {
echo "Could not Mail";
}
$scope.listItems 数据结构
$scope.listItems.customerName,
$scope.listItems.customerEmail,
$scope.listItems.customerPhone,
$scope.listItems.customerAddress
最佳答案
Working Demo
该演示将允许您使用真实的电子邮件服务器测试 php 脚本。
变化
foreach
旨在添加您的 $body
的循环变量,而是在每个循环周期中覆盖变量。要添加到 php 变量,请使用 .=
而不是 =
. $_POST
访问我的 php 数据. 使用
$_POST
访问您的 php 数据您需要更改 Angular Javascript 才能使用 transformRequest
(类似于这个 SO Answer )像这样:Javascript
$http({
method: "post",
url: 'http://wordpress.adriancarriger.com/stack-overflow-example-33591804/',
timeout: 20001,
transformRequest: function(obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: $scope.listItems,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(res) {
$scope.test.response = res.data;
console.log(res);
});
新 PHP
$root = $_SERVER['DOCUMENT_ROOT'];
require $root.'/vendor/autoload.php'; // I'm using composer (optional => you can remove this if you're not using composer)
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$list_items = $_POST;
$test_email = $list_items['testEmail'];
unset($list_items['testEmail']);
if (isset($list_items)) {
foreach ($list_items as $key => $item) {
$body .= '<br>'.$key.': '.$item;
}
//require("PHPMailer/class.phpmailer.php"); // you can uncomment this line if you're not using composer
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "<host on server>";
$mail->SMTPAuth = true;
$mail->Username = "so-examples@adriancarriger.com";
$mail->Password = "<password on server>";
$mail->From = "so-examples@adriancarriger.com";
$mail->FromName = "Adrian Carriger";
$mail->AddAddress($test_email, "Test Email");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "New Customer";
$mail->Body = $body;
$mail->AltBody = "Alternate Body";
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
else {
echo "Could not Mail";
}
关于angularjs - 在 PHPMailer 中处理 AngularJS 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591804/