Answer the question
In order to leave comments, you need to log in
Why is the function of sending emails via SMTP (WordPress) not working?
This function returns via Ajax:
).success(resp=> console.log('success '+JSON.parse(resp)));
//Uncaught SyntaxError: Unexpected token A in JSON at position 0
или
).success(resp=> console.log('success '+resp));
//success Array
/* Create NEW Mail*/
add_action( 'wp_ajax_mail_nps', 'mail_nps' );
function mail_nps(){
$nonce = $_POST['nonce'];
if (wp_verify_nonce($nonce, 'myajax-nonce')) {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
try {
global $current_user;
get_currentuserinfo();
$from=$current_user->user_email;
$to=$_POST['to'];
$subject=$_POST['subject'];
$message=$_POST['message'];
//$attachments = array(WP_CONTENT_DIR . $_POST['file']);
$headers = 'From: Aboriginal <'.$from.'>' . "\r\n";
$phpmailer = new PHPMailer;
$phpmailer->isSMTP();
$phpmailer->Host = 'email-smtp.us-east-1.amazonaws.com';
$phpmailer->SMTPAuth = true; // Force it to use Username and Password to authenticate
$phpmailer->Port = 25;
$phpmailer->Username = 'AKIAJ2x';
$phpmailer->Password = "93nUqA1xxx";
//Set the Email
$phpmailer->AddAddress('[email protected]', 'Test');
$phpmailer->SetFrom($from, 'XXX');
$phpmailer->Subject = $headers;
$phpmailer->MsgHTML($message);
//$phpmailer->AddAttachment($attachments);
$success=$phpmailer->send();
//send the message, check for errors
if ($success) {
printf(['mailError' => false, 'message' => 'email sent! ' . $phpmailer->ErrorInfo] );
} else {
printf(['mailError' => true, 'message' => 'email error! ' . $phpmailer->ErrorInfo]);
}
} catch (phpmailerException $e) {
printf(['mailError' => false, 'message' => 'email error! ' . $e->errorMessage()] ); //Pretty error messages from PHPMailer
} catch (Exception $e) {
printf(['mailError' => false, 'message' => 'email error! ' . $e->getMessage()]); //Boring error messages from anything else!
}
}
exit();
}
Answer the question
In order to leave comments, you need to log in
Thank you all for your help.
The code is correct, everything was decided by creating a new user.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question