Answers.Body: <p>This caught me out too. It <a href="https://craftcms.com/docs/3.x/config/#mailer-component" rel="nofollow noreferrer">says in the docs</a> that</p>
  <p><code>Any changes you make to the Mailer component from config/app.php will not be reflected when testing email settings from Settings → Email.</code></p>
  <p><a href="https://i.sstatic.net/vKcjQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vKcjQ.png" alt="Any changes you make to the Mailer component from config/app.php will not be reflected when testing email settings from Settings → Email." /></a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-03T09:15:35.963Z
Answers.Text: ‍♂️ good catch...I wasn't even referencing the Craft docs in this instance
Answers.UserDisplayName: Steven Grant
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-02T17:07:31.453Z
Answers.LastActivityDate: 2022-03-02T17:07:31.453Z
Answers.UserDisplayName: James Smith
Body: <p>I'm trying to set this up changing <code>config/app.php</code></p>
  <p>with</p>
  <pre class="lang-php prettyprint-override"><code>
  'dev' =&gt; [
          'components' =&gt; [
              'mailer' =&gt; function () {
                  // Get the stored email settings
                  $settings = App::mailSettings();
  
                  // Override the transport adapter class
                  $settings-&gt;transportType = Smtp::class;
  
                  // Override the transport adapter settings
                  $settings-&gt;transportSettings = [
                      'host' =&gt; getenv('SMTP_HOSTNAME'),
                      'port' =&gt; getenv('SMTP_PORT'),
                      'useAuthentication' =&gt; (bool)getenv('SMTP_USE_AUTH') ?: true,
                      'username' =&gt; getenv('SMTP_USERNAME'),
                      'password' =&gt; getenv('SMTP_PASSWORD'),
                      'encryptionMethod' =&gt; getenv('SMTP_ENCRYPTION_METHOD') ?: null, // 'ssl' or 'tls' or nothing
                      'timeout' =&gt; getenv('SMTP_TIMEOUT') ?: 10,
                  ];
  
                  // Create a Mailer component config with these settings
                  $config = App::mailerConfig($settings);
  
                  // Instantiate and return it
                  return Craft::createObject($config);
              },
          ],
      ],
  
      'production' =&gt; [
          'components' =&gt; [
              'mailer' =&gt; function () {
                  // Get the stored email settings
                  $settings = Craft::$app-&gt;systemSettings-&gt;getEmailSettings();
  
                  // Override the transport adapter class
                  $settings-&gt;transportType = MailgunAdapter::class;
  
                  // Override the transport adapter settings
                  $settings-&gt;transportSettings = [
                      'domain' =&gt; App::env('CRAFT_MAILGUN_DOMAIN'),
                      'apiKey' =&gt; App::env('CRAFT_MAILGUN_API_KEY'),
                  ];
  
                  return MailerHelper::createMailer($settings);
              }
          ]
      ],
  </code></pre>
  <p>The control panel tells me <code>It looks like these settings are being overridden by /Users/steven/Sites/xxx/config/app.php.</code> but when I test the email, it always wants to go out via Mailgun rather than the local mail server configured for dev.</p>
  <p>What have I missed here? My env has everything it needs and is in dev mode.</p>
  
Tags: <config><emails><environments>
Title: email settings per environment not working
ThreadText: # email settings per environment not working
  
  (Asked by: username_0 on 2022-03-02)
  
  I'm trying to set this up changing `config/app.php`
  
  with
  
  ```php
  
  'dev' => [
          'components' => [
              'mailer' => function () {
                  // Get the stored email settings
                  $settings = App::mailSettings();
  
                  // Override the transport adapter class
                  $settings->transportType = Smtp::class;
  
                  // Override the transport adapter settings
                  $settings->transportSettings = [
                      'host' => getenv('SMTP_HOSTNAME'),
                      'port' => getenv('SMTP_PORT'),
                      'useAuthentication' => (bool)getenv('SMTP_USE_AUTH') ?: true,
                      'username' => getenv('SMTP_USERNAME'),
                      'password' => getenv('SMTP_PASSWORD'),
                      'encryptionMethod' => getenv('SMTP_ENCRYPTION_METHOD') ?: null, // 'ssl' or 'tls' or nothing
                      'timeout' => getenv('SMTP_TIMEOUT') ?: 10,
                  ];
  
                  // Create a Mailer component config with these settings
                  $config = App::mailerConfig($settings);
  
                  // Instantiate and return it
                  return Craft::createObject($config);
              },
          ],
      ],
  
      'production' => [
          'components' => [
              'mailer' => function () {
                  // Get the stored email settings
                  $settings = Craft::$app->systemSettings->getEmailSettings();
  
                  // Override the transport adapter class
                  $settings->transportType = MailgunAdapter::class;
  
                  // Override the transport adapter settings
                  $settings->transportSettings = [
                      'domain' => App::env('CRAFT_MAILGUN_DOMAIN'),
                      'apiKey' => App::env('CRAFT_MAILGUN_API_KEY'),
                  ];
  
                  return MailerHelper::createMailer($settings);
              }
          ]
      ],
  
  ```
  
  The control panel tells me `It looks like these settings are being overridden by /Users/username_0/Sites/xxx/config/app.php.` but when I test the email, it always wants to go out via Mailgun rather than the local mail server configured for dev.
  
  What have I missed here? My env has everything it needs and is in dev mode.
  
  **Tags:** config, emails, environments
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-02. Score: 3 | Accepted answer
  
  This caught me out too. It [says in the docs](https://craftcms.com/docs/3.x/config/#mailer-component) that
  
  `Any changes you make to the Mailer component from config/app.php will not be reflected when testing email settings from Settings → Email.`
  
  [![Any changes you make to the Mailer component from config/app.php will not be reflected when testing email settings from Settings → Email.](https://i.sstatic.net/vKcjQ.png)](https://i.sstatic.net/vKcjQ.png)
  
  > **Comments:**
  > **username_0**: ‍♂️ good catch...I wasn't even referencing the Craft docs in this instance  
  
  ---
  
  