AngularJS array handling in PHPMailer

I am creating an application using AngularJS and PHPMailer. I need to pass an array from my Angular application to PHPMailer, and then get the array in PHPMailer and send 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 consisting of header fields such as client name, client email, client phone, etc. and lots of data under each heading. So it comes down to $ scope.listItems.customerName, $ scope.listItems.customerPhone, etc. I get data in Angular very easily, using Angular for everyone.

I managed to transfer the data to PHPMailer, but I have no idea how to get the data into PHPMailer and send it by mail.

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 = " 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 datastructue

 $scope.listItems.customerName, $scope.listItems.customerEmail, $scope.listItems.customerPhone, $scope.listItems.customerAddress 
+5
source share
2 answers

Working demo

The demo will allow you to test the php script using a real mail server.

Changes

  • You had a foreach that was supposed to add your $body variable, but instead, it rewrote the variable during each loop of the loop. To add php to a variable, use .= Instead of = .
  • I like to access my php data using $_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

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); }); 

New 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"; } 
+2
source

It is not right:

 $request = json_decode($postdata); foreach ($order as $value) { $body=$order; } 

This will overwrite the contents of $ body every time. You need to add to it:

 $body = ''; $request = json_decode($postdata); foreach ($order as $value) { $body .= $order . "\n"; } 
0
source

Source: https://habr.com/ru/post/1235454/


All Articles