WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 4 months ago

#17305 closed defect (bug) (fixed)

wp_mail() does not accommodate address format "Name <address@tld.com>"

Reported by: dllh Owned by: westi
Milestone: 3.2 Priority: normal
Severity: normal Version: 3.2
Component: Mail Keywords: has-patch
Focuses: Cc:

Description

The wp_mail() function does not accommodate addresses in the form "Name <address@…>". The phpmailer class chokes on this format, as the whole string is passed to its $address parameter rather than the constituent parts being passed as $address and $name. This does not likely affect a great many mailer functions in core, but it does stand to affect plugins (and does affect some wpcom plugins). The attached patch resolves the issue.

If I can provide any further information to increase the likelihood of landing this pretty quickly so that it can land on wpcom, please speak up.

Attachments (1)

17305.patch (670 bytes) - added by dllh 3 years ago.
Adds check for "Foo <a@…>" format.

Download all attachments as: .zip

Change History (15)

dllh3 years ago

Adds check for "Foo <a@…>" format.

comment:1 kawauso3 years ago

  • Keywords has-patch added

comment:2 dllh3 years ago

  • Cc daryl@… removed

comment:3 dllh3 years ago

  • Cc daryl@… added

comment:4 kardotim3 years ago

  • Cc kardotim added

comment:5 knutsp3 years ago

  • Cc knut@… added

comment:6 westi3 years ago

  • Owner set to westi
  • Status changed from new to assigned

Seeing as mail() supports RFC2822 addressing there should be no reason why we don't

Looking into this to see if it is a regression from the new PHPMailer.

comment:7 westi3 years ago

Test cases: [UT359]

comment:8 westi3 years ago

While writing the above test cases I discovered that we also need to apply this same logic to bcc and cc addresses :)

comment:9 westi3 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In [18006]:

Update wp_mail to correctly call the Address adding functions on PHPMailer for To, CC, BCC in a way which preserves our support for full RFC2822 address specifications.
Older versions of PHPMailer were not too careful about validating what we passed in to them as a plain email address - the new version expects we pass in the Name and Email address seperately.
Fixes #17305 based on a patch from dllh.

comment:10 westi3 years ago

  • Milestone changed from Awaiting Review to 3.2
  • Version changed from 3.1 to 3.2

comment:11 kitchin3 years ago

  • Cc kitchin added
  • Keywords needs-codex added

I am adding "needs-codex" so users can see the new feature.

This landed patch works, but could have been better. The regex has a part which does not work. The problem is fixed later in AddAnAddress(), so there is no real bug.

Details: The "\s?" in the regex does nothing, due to the greedy "+" beforehand (minor exception below). In the typical case, "Foo <bar@…>", the $recipient_name is right-padded: 'Foo ' instead of 'Foo'.

The string is trimmed later, after $phpmailer->AddAddress(), or AddCc(), or AddBcc(), call AddAnAddress(), which trims both the address and the name.

Several fixes are possible for this bad looking code, with no change in behavior. The best is to simplify the regexes to '/(.+)<(.+)>/' and call trim() in both arguments (or neither):

$phpmailer->AddAddress( trim($recipient), $recipient_name );

...AddCc...

...AddBcc...

That has no change in behavior.

An ENHANCEMENT would be to allow "<bar@…>" by making the regexes: '/(.*)<(.+)>/'.

Minor exception above: the "\s?" in the regex will catch a single "\n". But that is not a documented feature of wp_mail().

comment:12 follow-up: pavelevap3 years ago

  • Cc pavelevap@… added

I am not sure if I understand it well, but in WP 3.2 my headers with Bcc adresses with format "<address@...>" stopped working. I had to remove brackets and new format "address@..." works well now...

comment:13 in reply to: ↑ 12 kitchin3 years ago

OK, filed Ticket #18463 wp_mail() does not allow"<address@…>", regression, since it appears the format "<address@…>" worked before this bug, and my regex would restore that feature.

Replying to pavelevap:

I am not sure if I understand it well, but in WP 3.2 my headers with Bcc adresses with format "<address@...>" stopped working. I had to remove brackets and new format "address@..." works well now...

Last edited 3 years ago by kitchin (previous) (diff)

comment:14 bcworkz4 months ago

  • Keywords needs-codex removed

Added section to Codex with addressing examples

Note: See TracTickets for help on using tickets.