Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: MAIL_ENVELOP_FROM constant for flexible email Sender header mana… #8299

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/wp-includes/pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
$from_name = apply_filters( 'wp_mail_from_name', $from_name );

try {
// Configure envelop-from via MAIL_ENVELOP_FROM if defined.
if ( defined( 'MAIL_ENVELOP_FROM' ) && MAIL_ENVELOP_FROM ) {
$phpmailer->Sender = MAIL_ENVELOP_FROM;
}

$phpmailer->setFrom( $from_email, $from_name, false );
} catch ( PHPMailer\PHPMailer\Exception $e ) {
$mail_error_data = compact( 'to', 'subject', 'message', 'headers', 'attachments' );
Expand Down
30 changes: 30 additions & 0 deletions tests/phpunit/tests/pluggable/wpMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,34 @@ public function test_wp_mail_resets_properties() {
$phpmailer = $GLOBALS['phpmailer'];
$this->assertNotSame( 'user1', $phpmailer->AltBody );
}

/**
* Test wp_mail without MAIL_ENVELOP_FROM definition.
*/
public function test_wp_mail_without_mail_envelop_from() {
if ( defined( 'MAIL_ENVELOP_FROM' ) ) {
$this->markTestSkipped( 'MAIL_ENVELOP_FROM is defined, cannot test without it.' );
}

wp_mail( '[email protected]', 'Subject', 'Content' );

$mailer = tests_retrieve_phpmailer_instance();

$this->assertEmpty( $mailer->Sender, 'Sender field should be empty when MAIL_ENVELOP_FROM is not defined.' );
}

/**
* Test the MAIL_ENVELOP_FROM functionality in wp_mail.
*/
public function test_wp_mail_with_mail_envelop_from() {
if ( ! defined( 'MAIL_ENVELOP_FROM' ) ) {
define( 'MAIL_ENVELOP_FROM', '[email protected]' );
}

wp_mail( '[email protected]', 'Subject', 'Content' );

$mailer = tests_retrieve_phpmailer_instance();

$this->assertSame( '[email protected]', $mailer->Sender, 'MAIL_ENVELOP_FROM should correctly set the Sender field.' );
}
}
Loading