0

When I use the wp_mail() or mail() function in AJAX, the function doesn't work and doesn't send email.

When I use these functions simply in my functions.php file it works fine.

It's not related to WordPress functions or actions. I have checked everything, including using SMTP mail. SMTP mail works same as mail() function and it doesn't send in AJAX mode.

This is my functions.php file content:

add_action( 'phpmailer_init', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init( PHPMailer $phpmailer ) {
    $phpmailer->Host = 'godadysmtpsampledomain.secureserver.net';
    $phpmailer->Port = 465; // could be different
    $phpmailer->Username = 'sender@mysite.com'; // if required
    $phpmailer->Password = '12345'; // if required
    $phpmailer->SMTPAuth = true; // if required
    $phpmailer->SMTPSecure = 'ssl'; // enable if required, 'tls' is another possible value

    $phpmailer->IsSMTP();

}
// This code works fine;
wp_mail("hossein@mydomain.net", "test", "test");

But other codes or plugin in AJAX mode like Contact Form 7 don't work, it means the email doesn't delivered. I checked these plugins and wp_mail() returns true but the email doesn't delivered.

  • 1
    Please provide minimal workable code. – Fayaz Jan 28 '17 at 13:57
0

@T.Todua :

Ok and when I use your way it doesn't deliver the email. It doesn't send email in Ajax function and it's my problem exactly

add_action( 'wp_ajax_ABCABCABC', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init() { 

//it doesn't send/deliver in a Ajax function
wp_mail("hossein@mydomain.net", "test", "test");

 }

But when I use wp_mail() in post-back page it works fine for example when put it in functions.php file it sends/delivers the email:

//functions.php
//It sends in this mode
wp_mail("hossein@mydomain.net", "test", "test");

It's my problem.

| improve this answer | |
0

Too late, but perhaps can help others. I don't recommend to change SMTP data within code, because:

  • SMTP connection data is variable. If you work on localhost, probably you want to send test e-mails (eg using mailcacher). With this common scenario, you will have 2 SMTP data and can be confusing when sending files to the production server;
  • Is not recommended to put passwords in code, for security reasons. If you work with many people in the project, it will share your password;

There are a good solution to change SMTP data. Is the lightweight plugin Easy WP SMTP. With this plugin, you set your SMTP data in Wordpress database.

As each environment has your own database (production server, dev 1, dev 2 etc), you can set the SMTP data in each WP Admin you use.

In production server, set the real SMTP. In development servers (localhost), you can set your test SMTP data.

| improve this answer | |
-1

what is this?

add_action( 'phpmailer_init', ...........

you should use:

add_action( 'wp_ajax_ABCABCABC', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init() { ....... }

and in JAVASCRIPT call, do like this :

<script type="text/javascript" >
jQuery(document).ready(function($) {

    var data = {
        'action': 'ABCABCABC',
        'whatever': 1234
    };

    jQuery.post(ajaxurl, data, function(response) {
        //alert('Response got!! : ' + response);
    });
});
</script>

(source).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.