craftcms / craftcms/cms

Error When 'System Email Address' set via environment variable

Open
#7,654 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
3.6k
Forks
705
Avg merge
1d 5h
Merged PRs (30d)
134

Description

Description

Emails do not send when the System Email Address is set via an environment variable because Swiftmailer appears to try to validate the true setting value before the environment variable gets evaluated.

2021-03-04 07:53:28 [-][-][o8q3812r4fbtucra6c85hhm1fb][error][Swift_RfcComplianceException] Swift_RfcComplianceException: Address in mailbox given [$CRAFTENV_SYSTEM_EMAIL_ADDRESS] does not comply with RFC 2822, 3.6.2. in /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php:355
Stack trace:
#0 /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(272): Swift_Mime_Headers_MailboxHeader->assertValidAddress('$CRAFTENV_SYSTE...')
#1 /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(117): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array)
#2 /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php(74): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array)
#3 /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleMimeEntity.php(600): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array)
#4 /home/site/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/SimpleMessage.php(213): Swift_Mime_SimpleMimeEntity->setHeaderFieldModel('From', Array)
#5 /home/site/vendor/yiisoft/yii2-swiftmailer/src/Message.php(103): Swift_Mime_SimpleMessage->setFrom(Array)
Steps to reproduce
  1. Define an environment variable - $CRAFTENV_SYSTEM_EMAIL_ADDRESS. (NOTE: We use the .env.php method of setting environment variables which seems to work for all other site settings where we use environment variables.)
  2. Set the System Email Address setting to that environment variable in the control panel.
  3. Attempt to send an email via PHP module/plugin using the following code to initialize the Message object and the "from" header.
$this->emailSettings = Craft::$app->systemSettings->getSettings('email');
...
$message = new Message();
$message->setFrom([$this->emailSettings['fromEmail'] => $this->emailSettings['fromName']]);

Perhaps there is another way to load system settings with the environment variables already evaluated?

Additional info
  • Craft version: 3.6.6
  • PHP version: 7.3.27
  • Database driver & version: MySQL 5.7.33

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the email settings value from Craft::$app->systemSettings->getSettings('email') into yiisoft/yii2-swiftmailer/src/Message.php and the SwiftMailer mailbox validation shown in the stack trace. Reproduce the failure with an environment-variable System Email Address, then verify that the value is evaluated before SwiftMailer validates the From address and that sending succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.