[8.x] Mail empty address handling #39035
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is a follow-up to this discussion.
Short version: I have an optional
bcc
address set in config that all emailed messages are sent to. It works fine if that value is set, but if it's not, then Laravel just crashes. This PR makes all mail addressing functions (to
,cc
,bcc
,replyTo
,from
) handle empty addresses cleanly. In the existing Laravel code, this code will fail ifmail.bcc
is not set in config:Error:
The two patches (to
setAddress()
andhasRecipient()
) useempty()
to check the$address
parameter, so that all empty/falsy values are handled, includingfalse
,[]
,null
and''
.Alternatives to this patch are not immediately obvious, for example, this doesn't work:
because the config will only fall through to the default if the config value is
null
(missing from .env altogether), not just empty (present, but without a value, as is common in.env.example
files). This works, but it's ugly and annoying to have to do every time, and you should not need to know the internals of the mail class in order to do this safely:I note that there are no tests for bad inputs, only good ones, so I have added tests for these bad cases, and they fail without this patch.
I also noticed that there are no tests at all for the
from()
andhasFrom()
methods, so I added a full set of those in the same style.