WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 3 weeks ago

#49687 new enhancement

wp_mail() - Why is no envelope sender defined?

Reported by: vbbp Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 5.4
Component: Mail Keywords: dev-feedback needs-testing
Focuses: Cc:

Description

As just figured out, some (mail) servers require the envelope sender to be set in order to accept outgoing emails.

For the PHP built-in mail() function (https://www.php.net/manual/en/function.mail.php) this can be done using the additional parameter (e.g. "-fwebmaster@…) as explained in example 4 of the PHP manual page.

In order to use that option and set the envelope sender in PHPmailer either the sender attribute of the phpmailer object needs to be set ($phpmailer->Sender = "sender@…") or when using the setFrom() method of PHPmailer, the 3rd parameter needs to be set to true (=default).

Unfortunately, the current implementation of wp_mail() explicitly sets that 3rd parameter to false, which prevents the envelope sender from being set (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/pluggable.php?rev=47494#L358).

Is there any reason for doing so and could we change L358 to

$phpmailer->setFrom( $from_email, $from_name, true );

?

Change History (6)

#1 @vbbp
6 months ago

Btw, a quick fix is the installation of the plugin provided at https://www.webdezign.co.uk/avoid-wordpress-emails-ending-spam-folder/, but the obvious question remains whether and why the envelope sender is on purpose NOT set.

#2 @vbbp
3 months ago

  • Keywords needs-testing added; 2nd-opinion has-patch removed

#3 @cbutlerjr
7 weeks ago

  • Type changed from defect (bug) to enhancement

It's quite likely that a suggestion of this nature would be that "this is plugin territory." wp_mail() is a pluggable function, and if it was necessary in a given instance, then you could implement a plugged version of the function.

I would agree, though that it's worth a review. But the other thing to consider is that WP 5.5. is including an upgrade to phpMailer. So it might be something that needs to be looked at in the course of that improvement.

#4 @SergeyBiryukov
4 weeks ago

#51206 was marked as a duplicate.

#5 in reply to: ↑ description @SergeyBiryukov
4 weeks ago

Hi there, welcome to WordPress Trac! Thanks for the report.

Replying to vbbp:

Unfortunately, the current implementation of wp_mail() explicitly sets that 3rd parameter to false, which prevents the envelope sender from being set (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/pluggable.php?rev=47494#L358).

Just noting that this was an intentional change in [38286] / #37736.

#6 @lordandy1984
3 weeks ago

Not sure why this was added, because both options might cause issues on some environments (although I would stick with the PhpMailer default).

So the only solution is to use the phpmailer-init action in a small plugin?

<?php
add_action( 'phpmailer_init', 'xyz_add_phpmailer_setfrom' );

function xyz_add_phpmailer_setfrom( $phpmailer ) {
  $phpmailer->setFrom(
    'mysender@example.com',
    'My Sender' // From name.
  );
}
Note: See TracTickets for help on using tickets.