1

We have an AJAX handler called by jQuery.ajax() that takes a document stored on the server and mails it to the user. At the bottom there is a call to wp_mail like so:

$status_wpmail = wp_mail($recipient, $subject, $body, $headers, $filename);

Problem is the email never gets sent out, and any code after the wp_mail call never gets parsed, including a debug statement to dump out the value of $status_wpmail. The jqXHR response sent back to the page reports 'success' however.

I put in debug statements inside the handler to dump comments to a text file, and have confirmed statements are dumped to file right up to the wp_mail call. I've also enabled debugging statements per this link but I don't get any debug logs generated in wp-content.

Using a separate script to test wp_mail with a simple message and attachment I confirmed that wp_mail does work. I've also ruled out mail() as the culprit by installing the WP Mail SMTP plugin where this behaviour continues to persist.

Any ideas?

EDIT: I've opted for a workaround by directly calling phpmailer and skipping wp_mail. The message successfully sent after doing that. Still would be nice if anyone knows why wp_mail would hang like that.

4
  • have you tried it without an attachment?
    – Milo
    Oct 1 '14 at 22:54
  • Yup I've tried it without an attachment and also hardcoding in values.
    – Denis Wong
    Oct 2 '14 at 0:01
  • What exactly are you calling through your AJAX-Request? An PHP File or an WP Action?
    – lippoliv
    Oct 2 '14 at 7:19
  • Is your SMTP credentials set? Could you try setting them using these plugins and then use wp_mail(), WP SMTP - wordpress.org/plugins/wp-smtp WP Mail SMTP - wordpress.org/plugins/wp-mail-smtp
    – Domain
    Oct 4 '14 at 6:48
0

You can do some simple debugging:

  1. Try: wp_mail('youremail@com', '123', '123'); // to see if it will go through
  2. If #1 is not, check if you setup SMTP correctly // install a smpt plugin(wp smtp mail/easy wp smtp) and send a test email
  3. If you can't send out a test email by plugin, check if your email account has correct rights to send out email from third party (like yahoo mail requires "generate an app password", etc https://wpmailsmtp.com/docs/how-to-set-up-the-other-smtp-mailer-in-wp-mail-smtp/#secure-yahoo)
  4. If you use correct method to configure your email settings, but email still doesn't go out, check if your web server blocks the port 25, 465 and 587 (telnet the email server with these ports)

My issue is wp_mail is blocking "Reply-To" option in $headers now, everything works fine from my AJAX request as long as "Reply-To" option is removed from $headers. Not sure if you have same issue.

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.