I am building up an application using AngularJS and PHPMailer. I need to pass an array from my Angular application to PHPMailer and then retrieve the array in PHPMailer and mail the data to the client.
$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);
});
Here, $scope.listItems is an array which consists of head fields like customer name, customer email, customer phone etc and lots of data under each head. So it boils down to like $scope.listItems.customerName, $scope.listItems.customerPhone etc. I retrieve the data in Angular very easily using angular forEach.
i have been successful in passing the data to PHPMailer, but i have got no idea of how to retrieve the data in PHPMailer and mail it forward.
UPDATE
PHPMailer Code
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 = "[email protected]";
$mail->Password = "support@123";
$mail->From = "[email protected]";
$mail->FromName = "uvw";
$mail->AddAddress("[email protected]", "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 datastructue
$scope.listItems.customerName,
$scope.listItems.customerEmail,
$scope.listItems.customerPhone,
$scope.listItems.customerAddress
The demo will allow you to test the php script using a real email server.
foreach
loop that was intended to add the your $body
variable, but instead it was overwriting the variable during each loop cycle. To add to a php variable use .=
instead of =
.$_POST
.To access your php data using $_POST
you will need to change your Angular Javascript to use transformRequest
(similar to this SO Answer) like this:
$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);
});
$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 = "[email protected]";
$mail->Password = "<password on server>";
$mail->From = "[email protected]";
$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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With