我一直在尝试将HTML数据从Angular发布到PHPMailer作为JSON对象并在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);
})
};
Angular Service
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!";
}
?>
答案 0 :(得分:0)
问题之一可能是您使用'
引用 - 在
<h2><a href='/index.html'> Div Contents with a tag </a><h2>
会干扰JSON引号,因此如果您尝试使用以下内容:
$scope.documentContents = "<h2><a href='/index.html'> Div Contents with a tag </a><h2>";
$scope.dataToSend = {
person: $scope.Fullname,
contents: $scope.documentContents.replace(/'/g, '"');
};