Skip to content

[9.x] Add X headers when using Mail::alwaysTo#41101

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
morrislaptop:always-to-x-headers
Feb 18, 2022
Merged

[9.x] Add X headers when using Mail::alwaysTo#41101
taylorotwell merged 4 commits into
laravel:9.xfrom
morrislaptop:always-to-x-headers

Conversation

@morrislaptop
Copy link
Copy Markdown
Contributor

When using Mail::alwaysTo in development environments, the original To, Cc and Bcc are lost. This makes it difficult to determine where the email was going to during testing.

This PR adds the original to, cc and bcc into X-Headers in the email so this information can be retrieved, while still preventing the email being sent to these recipients.

The below screenshot of Helo shows alwaysTo being set, an attempt to send an email with To, Cc, Bcc and the resultant email with the added headers.

image

$this->assertStringContainsString('X-Cc: dries@laravel.com', $sentMessage->toString());
$this->assertStringContainsString('X-Bcc: james@laravel.com', $sentMessage->toString());
$this->assertFalse($recipients->contains('nuno@laravel.com'));
$this->assertFalse($recipients->contains('dries@laravel.com'));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be a bit carefully here because we're not actually checking anymore if the CC: and BCC: headers are not present anymore (something the original tests implicitly did). I don't think BCC recipients, for example, are listed in the recipients of an envelope to begin with?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah ok i'll add those checks back in, with the specific headers

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BCC won't be in the string (but this is tested by checking the list of recipients)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints Specifically checking for these headers now 👍 Confirmed that the BCC address is normally returned with $sentMessage->getEnvelope()->getRecipients()

Comment thread tests/Mail/MailMailerTest.php

$sentMessage = $mailer->send('foo', ['data'], function (Message $message) {
$message->from('hello@laravel.com');
$message->to('nuno@laravel.com');
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this for extra measure

@taylorotwell taylorotwell merged commit 18a996f into laravel:9.x Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants