PHPMailer and Angular 8

Viewed 719

Working with Angular 8 and PHP (ver 7) and PHPMailer (latest version)

If I enter text as a $variable, it works fine. It I use Angulars json POST, the console shows the data sent, but I get this error message:

Invalid address: (to): Message could not be sent. Mailer Error: Invalid address: (to):

Any insights or thoughts are greatly appreciated.

Thank you

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
error_reporting(E_ALL);
//error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$subject = $request->subject;
@$sentfrom = $request->sentfrom;
@$sentto = $request->sentto;
@$body = $request->body;
@$cc = $request->cc;
@$bcc = $request->bcc;    
@$attachments = $request->attachments;


$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'MYHOST.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'info@MYHOST.com';                 // SMTP username
    $mail->Password = 'ItsaSecret%#!';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients

    $mail->addAddress($sentto);

    $mail->setFrom('info@MYHOST.com', '');
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = (string)$subject;
    $mail->Body = (string)$body;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
0 Answers
Related