Problem/Motivation
Steps to reproduce
- Enable SMTP module and configure it.
- Enable workbench or any other module that triggers an email notification on node save.
- Save the node and you will get an error i.e.
The website encountered an unexpected error.
Proposed resolution
In SMTPMailSystem.php, the below code causes this issue:
case 'cc':
$cc_recipients = explode(',', $value);
foreach ($cc_recipients as $cc_recipient) {
$cc_comp = $this->getComponents($cc_recipient);
$mailer->AddCC($cc_comp['email'], $cc_comp['name']);
}
break;
case 'bcc':
$bcc_recipients = explode(',', $value);
foreach ($bcc_recipients as $bcc_recipient) {
$bcc_comp = $this->getComponents($bcc_recipient);
$mailer->AddBCC($bcc_comp['email'], Unicode::mimeHeaderEncode($bcc_comp['name']));
}
break;
There is no empty check for $value that results in the below error:
Drupal\Core\Entity\EntityStorageException: Invalid address: (cc): in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 846 of /var/www/d8/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
PHPMailer\PHPMailer\Exception: Invalid address: (cc): in PHPMailer\PHPMailer\PHPMailer->addOrEnqueueAnAddress() (line 1081 of /var/www/d8/vendor/phpmailer/phpmailer/src/PHPMailer.php).
The attached patch solves the problem. It checks for the NULL/Empty value and if it's empty then it doesn't execute the PHPMailer code.
Comments
Comment #2
pushpinderchauhan commentedComment #3
pushpinderchauhan commentedComment #4
pushpinderchauhan commentedComment #5
tr commentedSeems like the wrong fix.
The headers are looped over like this:
foreach ($headers as $key => $value)A valid email message MUST have a $value for each $key. I don't know why $value isn't set for some keys, but the $value should be checked at this point, even before the switch begins. It shouldn't be checked in each individual case, like you do in the patch - note there are other cases in that switch that should have $value checked which you didn't fix. If you check $value before the switch, that's basically one line of code that needs to be added. If $value is not set, $headers[$key] should probably be unset so we don't send invalid headers to PHPMailer.
But I think it's also worth the effort to trace back and see how the Cc: header is getting set with no value - it's quite possible that your module which is sending email is passing wrong values, and that should be fixed.
Likewise, to make this module more robust, a test should be added to check that sending a header without a $value will work without error.
Comment #6
pushpinderchauhan commentedThanks, TR. I agree with you. The origin of the issue is the
content_moderation_notifications modulewhich is passing empty cc & bcc keys to header array. I'll take care of that module issue later and post a patch for that too.Eariler I wasn't sure whether
$valueis mandatory or not but after digging in the code it seems we may apply explicit check for this. Giving it a try with another patch.Comment #8
pushpinderchauhan commentedComment #9
tr commentedThanks for the test, that's very important.
When you add a test you should post a patch with only the test in it - that patch should FAIL, and that will demonstrate that the test is really finding the bug. Also post a second patch with both the test AND the fix, which will demonstrate that the proposed fix actually fixes the bug.
It's hard to review your patch in #8 because so many lines are changed. The fix I had in mind is more like this:
This checks the $value without having to reformat all the switch code, and adds a comment saying what is being done and why. This makes it much easier to read and to see what is being done, which makes it easier to understand and maintain.
Here is a new patch using the above fix, and correcting some minor coding standards problems with your test. I've also uploaded the test separately to see if it will fail without the fix.
Comment #11
tr commentedThe results in #9 are exactly what were expected:
Comment #12
pushpinderchauhan commentedGreat... Thanks for the detailed explanation and your support in this issue. It really helps!
Since you have posted the patch now and it works perfectly so moving it to RTBC.
Comment #13
andrew answer commentedPatch re-rolled for last dev version.
Comment #15
andrew answer commentedTest failed, but if I'll apply https://www.drupal.org/project/smtp/issues/3191046#comment-14026470 together with this patch, it will be finished successfully.
Comment #16
andrew answer commentedComment #17
andrew answer commentedI confirm that two patches works together; +1 to release.
Comment #19
japerryThanks for the patch and the tests! Committed.
Comment #21
rivimeyHave no idea if it is in fact related, but I've just entered a bug #3304401: From address considered invalid which relates to email address validation and was introduced in the v1.1 release.