Adding Notification email address does not overwrite the system admin email from address
When I enter a Notification email address on the account settings page (/admin/config/people/accounts) the from address for system notifications remains the email address of the system administrator (user 1). This is not, what the description of Notification email address field suggests:
The email address to be used as the 'from' address for all account notifications listed below. If 'Visitors, but administrator approval is required' is selected above, a notification email will also be sent to this address for any new registrations. Leave empty to use the default system email address (system.admin@mydomain.com).
Steps to reproduce
- Clean install of Drupal 9.1.4.
- Enter Notification email address (/admin/config/people/accounts)
- Register new user (Visitors, but administrator approval is required)
- Receive notification with from email address of system admin (not from Notification email address as expected)
Proposed resolution
- Keep system admin email address (user one) for system maintenance and development.
- Make it possible for user ≥2 with administration role to approve new registrations.
Release note
There is a field on the account settings configuration page for a notification email address to use as the sending email address for user account notifications. Previously, this setting was not respected and all emails were sent from the site administration email address instead. Starting with Drupal 10.1.0, the setting will be respected. Site owners should note that the sender email address for user notifications will change if they have previously configured this address to something other than the site default email. Review Account notification email address configuration is now respected for more information.
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | interdiff_42-48.txt | 2.73 KB | anup.sinha |
| #48 | 3201472-48-notify_from_email_not_override_system_mail_fix.patch | 2.89 KB | anup.sinha |
| #43 | interdiff_38-42.txt | 3.18 KB | anup.sinha |
| #42 | 3201472-42-notify_from_email_not_override_system_mail_fix.patch | 4.29 KB | anup.sinha |
| #38 | interdiff_36-38.txt | 5.84 KB | anup.sinha |
Comments
Comment #2
Ralf Eisler commentedComment #3
cilefen commentedComment #6
christian deloach commentedComment #7
christian deloach commentedUpdated issue title. The system email address is NOT the user-1 email address (although, they could be set to the same value/email).
I confirm that when receiving password reset emails and new user notification emails, the From email address does not use the notification email address if set /admin/config/people/accounts and continues to fall back to the system email address set at /admin/config/system/site-information.
Comment #8
anup.sinha commentedHi All, the issue is present in Drupal 10 Dev version as well, so updating the Drupal version for this issue to Drupal 10 Dev. I have investigated the issue and found that the problem is with the logic written inside doMail() function [File path - web\core\lib\Drupal\Core\Mail\MailManager.php]. Here we are not fetching the data from the "Notification email address" field. As per the existing logic, we are just setting the default site mail address as "From: " address while sending mails and if this value is not there then we are checking in settings file.
So I have corrected the logic in order to fix this issue. As per my analysis, correct logic should be -
Attaching the patch for this issue. I have tested it from my end and it's working as expected. Request the community to validate my patch and let me know if there is any observation.
Comment #10
anup.sinha commentedHi All,
I had to modify the following test - "core\modules\user\tests\src\Functional\UserAdminTest.php", Method - "testNotificationEmailAddress" as in this functional test also we were matching the email from address with default system mail even after setting the "Notification email address" value. I have modified the test as per our new logic, so that after setting the notification mail address it will now match the from email address with the value set as "Notification email address" instead of default system mail. Request the community to validate the patch and let me know if there is any observation.
Thanks & Regards,
Anup
Comment #11
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
Can confirm the issue still exists on 10.1.x and that patch #10 addresses the issue.
Moving back to NW for additional test coverage.
Will need a scenario that shows what is sent when Notification email address is and isn't set. What email is received.
Thanks!
Comment #12
anup.sinha commentedHi @smustgrave,
Many thanks for moving it to the review queue. Now to answer your query, the exact same email will be triggered in both the cases. It's just that the from address will be different and this is already adjusted in UserAdminTest.php.
a) When notification email address is not set -> From address will be the default site email address set during Drupal installation (System->Basic Site settings).
b) When notification email address is set -> From address will be the "Notification Email address" set in Account settings.
Could you please let me know if any other scenarios need to be covered for this change?
Thanks & Regards,
Anup
Comment #13
smustgrave commentedThink those are the two scenarios. Also for tests if you can upload a tests-only patch and the full one together in that order. So we can see the red/green
Comment #14
anup.sinha commentedHi @smustgrave,
I have added a new test to cover the scenario when notification email address is not set. So now below are the two scenarios and their corresponding tests -
Test file - core\modules\user\tests\src\Functional\UserAdminTest.php
1. When notification email address is set -> Adjusted Test - testNotificationEmailAddress()
1. When notification email address is not set -> newly created Test - testNotificationEmailAddressNotSet()
Also attached test only patch (it will fail as expected as the fix is not included in the patch) and the full patch as requested. Please review the patch and let me know if there is any observation.
Thanks & Regards,
Anup
Comment #16
smustgrave commentedThanks! Removing the needs tests tag.
Manually tested the patch following the steps in the IS and confirmed the issue.
With patch confirmed the correct email is being used.
Comment #18
xjmThanks for working on this bug! There are just a few minor code style and comment issues to fix:
This has some grammatical oddities. I'd suggest:
Shouldn't we inject the config factory rather than using
\Drupal::config()?This comment needs to wrap at 80 characters. I'd also suggest it be changed to:
Otherwise, use the default site email address.This also is a bit grammatically weird. I'd suggest:
This should have a
voidreturn typehint.Why is the "es" in parentheses? Either we're testing one, or we're testing more than one. I also actually think this comment could be removed, since it sort of duplicates the docblock.
I'd suggest adding newlines above both these comments for readability.
$systemis a bit of a confusing variable name. I would change this to$site_configuration.In general, it's better to specify a specific, meaningful test value than to use
randomMachineName(). (Our best practice or this has changed over the years.This comment is wrapping too early. It should use as close to 80 chars as possible on the first line. I'd suggest:
Also, a newline above here would be good.
We can remove the third parameter from this assertion; we have moved away from using custom assertion messages in core as a best practice.
Comment #19
anup.sinha commentedHi @Jess,
Thanks for reviewing the changes and providing valuable feedbacks. I have created a new patch by addressing all the code review comments for both my new test function as well as existing "testNotificationEmailAddress" function.
Only the comment #5 not addressed as PHP Code sniffer throwing the below error if I am adding void return-type.
"If there is no return value for a function, there must not be a @return tag". Please check the rest of the changes in my new patch and let me know if there is any observation.
Thanks & Regards,
Anup
Comment #20
smustgrave commented@anup.sinha thank you for all the work on the ticket.
When making changes please include an interdiff between two patches so we can easier see the changes.
Also for the CI failure you can test locally with
./core/scripts/dev/commit-code-check.shComment #21
smustgrave commentedComment #22
_utsavsharma commentedTried to fix CCF for #19.
Comment #23
anup.sinha commentedHi @Utsav,
Thanks for fixing the "drupaluser" word. The build for #19 failed as this word is not there in dictionary.txt.
@smustgrave, PFA the interdiff between patch #14 and #19. I have just fixed all the code review comments provided by Jess in patch #19.
Thanks & Regards,
Anup
Comment #24
smustgrave commentedThanks @anup.sinha great work!
Comment #25
xjmThanks everyone!
It's slight scope creep to fix the existing method in the same way as the new method, but since it makes the test much more clear and consistent, we'll go ahead and commit it as-is.
Adding credit for @smustgrave for helpful and thorough reviews, @Ralf Eisler for reporting the issue, and @Christian DeLoach for manual testing and clarifying the exact bug.
I made some small comment fixes on commit:
Committed to 10.1.x. I considered backporting this, but it could be slightly disruptive, because the sending email address for many thousands of site account notification emails will change across sites that have this configured. So, I've kept it to 10.1.x only, and added a change record and a release note. I also added it to the release notes draft for 10.1 as per our new policy.
Comment #26
xjmComment #27
xjmComment #30
xjmSorry, in the course of reviewing my own release note and the CR, I realized a fundamental problem: The fix here is changing the sending address for all site emails when the setting is configured (including "site update available" notifications and the like), not just mails sent by the user account.
So, I think we need more structural fix to allow the mail manager to have the sender email set in a separate method, and allow the value to be set/injected by the caller.
Sorry! Uploading a patch from my commit that has the comment fix, but actually we need further work here. =/
Comment #31
xjmComment #32
anup.sinha commentedHi Jess,
Many thanks for pointing out the missed scenario. I have implemented a patch to restrict our new condition only for user operations without creating a new method.
> When user module is calling the mail() function from _user_mail_notify(), it's sending the module name (1st parameter) as "user".
> So I have added a check that our new condition will get executed only when the module name is "user", not for other scenarios
Could you please check this solution and let me know if it looks good to you.
Thanks & Regards,
Anup
Comment #33
xjmThanks @anup.sinha! That's a good start, and will at least avoid the regression that we almost caused. 😉
However, it has the disadvantage of putting logic related to the user module in a low-level core component. That's why I suggested making the sending mail address injectable into
MailManager.Tagging for framework manager feedback on the best architecture to use here.
Comment #34
larowlanWe shouldn't be special casing the user module here.
doMail fires both hook_mail and hook_mail_alter after this hunk, so this special-case logic should belong in user.module in it's hook_mail implementation
Comment #35
anup.sinha commentedHi Jess,
Here is my updated patch where the "From" email address has been passed from the user module - _user_mail_notify() function. The changes are as below, also attached the interdiff.
Right now we don't have any option to set the from address while using Drupal mail() function, so this will allow us to use our custom from address as well. Let me know your thoughts please on this patch. Otherwise, I can just alter the $message['headers']['FROM'] value in user_mail() function as suggested by Lee and can create a new patch.
Thanks & Regards,
Anup
Comment #36
anup.sinha commentedHere is an updated patch by fixing the error from previously uploaded patch.
Thanks,
Anup
Comment #37
larowlanWe don't need any of these changes, $params['from'] can be set
Comment #38
anup.sinha commentedHi Lee,
PFA the latest patch by altering the from address from hook_mail() in user.module. Also $params['from'] was not setting the from address, then by checking the doMail() function I found out that it's actually $message['from'] variable through which we can alter the "FROM:" address.
Request you to review this patch and let me know please if it looks good to you.
Thanks & Regards,
Anup
Comment #39
anup.sinha commentedHi Lee and Jess,
Can you please now commit this patch into Drupal 10 branch if everything looks good to you.
Thanks & Regards,
Anup
Comment #40
smustgrave commentedRetested #38 and I'm not longer receiving emails from my account email just from site email.
Comment #41
larowlanThanks, the approach is looking much cleaner, no API changes, no leaking of user.module into core services.
These changes are out of scope
It feels odd that we're adding a new test case for existing functionality, instead of retaining the existing test and adding a new test for the new functionality
Doesn't the default mail handler do this if we don't pass
$message['from']i.e. should we conditionally set from, only when mail_notification is set?
We're on the home straight now, thanks!
Comment #42
anup.sinha commentedHi Lee,
Thanks for reviewing the code. I agree with you that we should alter the value only when notify email is set. Also merged the new test into existing one. Here is my updated patch.
Also the changes which are out of scope are actually code review comments given by Jess in comment #18 for my new test. Like we should not use random function to generate username, in assertCount third message parameter is no longer used etc. So at that time I thought if we are fixing these issues in new test then fix those problems in existing one as well. Currently both the tests are merged, so I think these are in-scope now.
@smustgrave, could you please retest? Both the scenarios are covered in our unit test - 1. when notify mail is set, from address should be notify mail 2. when notify email is not set, from address should be site email.
Thanks & Regards,
Anup
Comment #43
anup.sinha commentedComment #44
smustgrave commentedRetested #42 and things are working as expected.
Changes from #41 also appear to be addressed
Comment #45
larowlanDoesn't look like #41 items 1 and 2 have been addressed
Comment #46
anup.sinha commentedHi @Lee,
Please find below my response -
Comment #41 - 2. As you can see in my new patch #42, the new test function testNotificationEmailAddressNotSet has been deleted and the new testcases have been included in the existing testNotificationEmailAddress test itself.
Comment #41 -1. As I mentioned in my comment #42 - The changes which are out of scope are actually code review comments given by Jess in comment #18 for my new test. Like we should not use random function to generate username, in assertCount third message parameter is no longer used etc. As now both the tests are merged, so I believe those code review comments given by Jess are in-scope now.
Please let me know if I should revert these changes, then I will create a new patch.
Thanks & Regards,
Anup
Comment #47
larowlanHi @anup.sinha I'm pretty sure @xjm made those comments in relation to new tests but would agree with me that changing existing tests is out of scope here. But I'll ask @xjm to confirm that.
FWIW I don't agree with using discrete values, as they can lead to collisions, I would always prefer using random values.
Comment #48
anup.sinha commentedHi @Lee,
Thanks for the update. I have reverted all the unwanted changes and created a new patch which contains changes required for this fix only. PFA the updated patch.
Thanks & Regards,
Anup
Comment #49
smustgrave commentedRetested #48 and the functionality is still there.
Appears the points by @larowlan in #47 have been addressed.
Comment #50
larowlanThanks, this looks 10x simpler/cleaner now.
I've queued a 9.5 test run, although I suspect that will fail because 9.5.x HEAD is failing due to ::upgradePhpUnit taking us all the way to PHPUnit 9.6 which has new deprecations.
Comment #51
larowlanRetesting 9.5.x now that HEAD is fixed
Comment #52
larowlanLooking at the current code-base there's a few things that stand out.
1) We don't have a default value for mail_notification in system.site, this feels like it breaks our standard contract for config objects, it should at least be null I think. I want to ask @alexpott about this. It will likely mean a follow-up issue.
2) There is an existing test for this functionality, as follows:
As you can see, it is testing that the emails have the notification email as the reply-to.
So perhaps the issue here is that the description is wrong, and it should say 'the reply-to' address.
This feature was added in #494518: Allow for custom user registration approval email address
The issue with having a different from address is the site might need to go through a verification process for that email address to be able to send email on behalf of that address, whilst the reply-to does not have that issue.
I will ask the User module maintainer for their thoughts.
Comment #53
moshe weitzman commentedI was summoned here as a User system maintainer.
Comment #54
smustgrave commentedSeems like we are a go. Opened a follow up for point 1.
Comment #55
larowlanDiscussed this with other committers and they agreed that the behaviour change here could have unwanted side-effects for sites.
e.g. in many hosting platforms, there is one validated from address for the whole site that has a raft of measures wound up in it to ensure that there is no issues with spam.
With this change, suddenly a second from address would require spam validation.
The consensus was to update the field description in said, so that its clear the notification email is used for the reply-to header, not the from.
So repurposing this issue to achieve that. It means we can back out all the changes and start afresh with just the #description change.
Thank folks for keeping this one moving.
Comment #58
larowlanCrediting @alexpott and @Gábor Hojtsy who I discussed this with
Comment #59
christian deloach commentedI appreciate all of the time and effort addressing this issue.
I just started a new project that required visitors to get admin approval when creating an account and came across a potential side effect that may cause problems with changing how this email address is used. As the description states, this field serves two purposes. Not only is it used as the "from" address of the account-related emails, which we now know is actually behaving as the "reply-to" address, but it's also the "to" address of the emails that get sent for new user account approval notification emails. Ideally, there would be three separate addresses: the "from" address (may be something like no-reply@servername.com), the "reply-to" address (may be something like info@companyname.com), and the "to" address for new user account approval notification emails (would be name-of-admin@companyname.com).
I agree with @larowlan's sentiment, changing this email address to be the "from" address may create unexpected problems for some, potentially reducing the delivery of account-related emails as they may be flagged as spam. Our "from" addresses are typically no-reply@servername.com to help improve mail delivery and avoid having to go through a verification process for a client's email address (e.g. info@companyname.com). Having (keeping) the field as the "reply-to" address allows us to send emails from a no-reply@servername.com email address, but if a recipient were to reply, their email may go to a valid client email address. More importantly, it allows us to use a valid client email address as the "to" address of the new user account approval notification emails.
Comment #61
quietone commentedThis was reverted but the CR remained published. I set the CR to draft.
Comment #63
znerol commentedFiled #3579025: Add an admin email field and deprecate user notification email and update email config.