【发布时间】:2015-11-19 16:27:56
【问题描述】:
我一直在尝试将来自 Angular 的 HTML 数据作为 JSON 对象发布到 PHPMailer 并在 PHP 上对其进行解码。但是我只能发送h1, h2.... p 元素,当我使用 div / a 等作为对象时,它会显示两个错误。 Anonymous function on posting 和 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:57948' is therefore not allowed access.
PHP 文件在远程服务器上。
这是我的代码:
角度控制器
$scope.documentContents = "<h2><a href='/index.html'> Div Contents with a tag </a><h2>"; // this gives error
// $scope.documentContents = "<h2> Div Contents with a tag <h2>"; this one works fine
$scope.sendEmailWithInfo = function(){
console.log('clicked');
$scope.dataToSend = {
person: $scope.Fullname,
contents: $scope.documentContents
};
console.log($scope.dataToSend.contents);
userServices.sendEmail($scope.dataToSend).success(function(response){
console.log(response);
})
};
角度服务
sendEmail: function (dataToSend) {
return $http.post('http://....remote.php', dataToSend, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}
PHP 代码
<?php
header('Access-Control-Allow-Origin: *');
require 'PHPMailer-master/PHPMailerAutoload.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$person = $request->person; //used in template
@$contents = $request->contents;
$otherContents = "<h2>This is h2 html content</h2>";
$otherPerson= "Other Person";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "email@email.com";
$mail->Password = "password";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
ob_start();
require("template.php");
$html = ob_get_contents();
ob_end_clean();
$mail->setFrom('email here', $person);
$mail->addReplyTo('email here', 'First Last');
$mail->addAddress('email', 'John Doe');
$mail->Subject = 'PHPMailer sendmail test';
$mail->msgHTML($html);
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
【问题讨论】:
标签: javascript php html angularjs phpmailer