N
N
Ninja Mate2016-09-01 23:19:20
SMTP
Ninja Mate, 2016-09-01 23:19:20

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

I tried these settings for the test to send emails through another plugin - everything works.
How to win?
/* 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

4 answer(s)
N
Ninja Mate, 2016-09-05
@victorzadorozhnyy

Thank you all for your help.
The code is correct, everything was decided by creating a new user.

D
Dmitry Grebenshchikov, 2016-09-02
@somanytears

the problem is with the hosting, write to him.

D
Dmitry, 2016-09-02
@b4rbed

Apparently free hosting without SMTP support.

D
Dmitry, 2016-09-02
@pandvm

Why don't you use the default wp_mail ? No, well, really interesting, any difficulties with it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question