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.