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


// 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

@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:

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

It's my problem.


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


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.