I have been trying to post HTML data from Angular to PHPMailer as JSON object and decoding them on PHP. However I can only send h1, h2.... p
elements, when I use div / a etc as an object, it show two errors. Anonymous function on posting
and No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:57948' is therefore not allowed access.
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 file is on the remote server.
Here is my code:
Angular controller
$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 Code
<?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!"; } ?>
One of the problem could be that quotes you use '
- in
<h2><a href='/index.html'> Div Contents with a tag </a><h2>
interferes with JSON quotes, so if your try to use following:
$scope.documentContents = "<h2><a href='/index.html'> Div Contents with a tag </a><h2>";
$scope.dataToSend = {
person: $scope.Fullname,
contents: $scope.documentContents.replace(/'/g, '"');
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.