Title: wp_mail() hiding recipient errors
Last modified: January 28, 2021

---

# wp_mail() hiding recipient errors

 *  [sporkme](https://wordpress.org/support/users/sporkme/)
 * (@sporkme)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/)
 * I wanted to run this by someone before possibly opening a bug…
 * After spending some time looking at why some mail-tags that allow replacement
   of from/to/and other headers in Contact Form 7 were not working (but not throwing
   an error), I started adding a bunch of debug statements in CF7. Everything looked
   fine, and where it called wp_mail(), there was no error.
 * I then moved on to adding more debug stuff in wp-includes/pluggable.php.
 * I followed things down to around line 260 where individual headers are parsed,
   and then down to line 303 where the reply-to is added. My debugging showed me
   that even though I was in that block within the switch statement, my “reply-to”
   was not being added. I was however able to see that I had the formatting wrong–
   there were multiple recipients, and I had “<foo1@example.com, [foo2@example.com](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/foo2@example.com?output_format=md)
   >” which got chunked into two addresses, “<foo1@example.com” and “foo2@example.
   com>”, neither of which passed PHPMailer’s validation.
 * My thought is that if there’s an error in adding any of these addresses, wp_mail()
   should pass it on up the chain. The whole routine is wrapped in “try/catch”, 
   but nothing is done with the error.
 * Is this perhaps considered a bug in that end users of anything that calls “wp_mail()”(
   core, plugins) would spend an inordinate amount of time tracking down why mail
   is not sending… Thoughts?
 * I also elaborated on this in the CF7 forum ([https://wordpress.org/support/topic/mail-tags-ignored-for-from-and-reply-to/?view=all](https://wordpress.org/support/topic/mail-tags-ignored-for-from-and-reply-to/?view=all)),
   if that’s at all helpful, but to be clear, I’m NOT asking about plugin help, 
   just wp_mail() itself and error handling…

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/#post-13976995)
 * Is this failing to send the message, or just failing to add poorly formatted 
   Reply-To: addresses and sending anyway? If the message fails to send, `false`
   ought to be returned so the caller can do something else. But if the message 
   is sent anyway, I don’t see address rejection as a reason to return an error.
 * So maybe not a “bug” per se, but I do agree that doing nothing is poor behavior.
   The address rejection is handled at line 414 with `continue;`, meaning it’s ignored.
   We ought to do more than that, but I don’t think returning an error is appropriate.
   At least log a warning message or something. This is a pluggable function, so
   we’re free to add whatever process we want ahead of `continue;`.
 * It would be nice to have an action hook inserted there so we could add in an 
   exception handler without having to plug in an entire replacement function.
 *  Thread Starter [sporkme](https://wordpress.org/support/users/sporkme/)
 * (@sporkme)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/#post-13999130)
 * Sorry I drifted away from this one…
 * Yes, the message is sent, but is missing the headers.
 * Personally, even just something, anything that would show up with debug enabled
   would be great. The error I tracked down ended up being really simple, but was
   nearly impossible to find by digging in logs and adding extra debugging in the
   plugin. I mean, maybe even displaying the error in all cases is worth something?
   If a user is expecting certain parameters to be set for the mail message and 
   those are not being set do to a syntax problem, it seems like telling them is
   generally a good thing?
 * PHPMailer does give a very nice and verbose answer when you screw up:
 *     ```
       in wp_mail() - address checker, adding reply-to as addr and name
       <css-info@example.com
       <strong>Invalid address:  (Reply-To): <css-info@example.com</strong><br />
       in wp_mail() - before switch() on address_header
       reply_to
       in wp_mail() - address checker, adding reply-to as addr and name
       css-chelsea@example.com>
       <strong>Invalid address:  (Reply-To): css-chelsea@example.com></strong><br />
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/#post-14003761)
 * I could see returning a status code instead of a boolean. 0 for complete failure,
   xFF for normal completion, something in between for sent but errors were encountered.
 * The simplest change would be to add a filter before the line 414 `continue;` 
   so we can easily implement our own solution if there’s a PHPmailer exception.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘wp_mail() hiding recipient errors’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/wp_mail-hiding-recipient-errors/#post-14003761)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
