<p>This was addressed in <a href="https://github.com/craftcms/cms/issues/2554" rel="nofollow noreferrer">this issue</a> and the <code>MailerHelper::createMailer()</code> method was deprecated as of Craft 3.0.18, <code>App::mailerConfig()</code> should be used instead.</p>
<p>Changelog: <a href="https://github.com/craftcms/cms/blob/v3/CHANGELOG.md#3018---2018-07-31" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/v3/CHANGELOG.md#3018---2018-07-31</a></p>
<p>Here's a working version for Craft 3.1.0 and above.</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

use craft\helpers\App;
use craft\mail\transportadapters\Smtp;

return [
    '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; 'xx',
                'port' =&gt; 'xx',
                'useAuthentication' =&gt; true,
                'username' =&gt; 'xx',
                'password' =&gt; 'xx'
            ];

            $config = App::mailerConfig($settings);
            return Craft::createObject($config);
        }
    ],
];
</code></pre>

CC BY-SA 4.0
2022-07-31T06:47:58.650Z
2022-07-31T06:47:58.650Z
Ben Croker
<p>For me what worked is instantiating and returning a Craft object, see below:
Also, you might want to consider abstracting away env variables.
I am using Craft 4</p>
<pre><code>return [

    'id' =&gt; App::env('CRAFT_APP_ID') ?: 'CraftCMS',

    'modules' =&gt; [

        'my-module' =&gt; \modules\Module::class,

    ],

    'components' =&gt; [

        'mailer' =&gt; function() {



            $settings = craft\helpers\App::mailSettings();

            $settings-&gt;fromEmail = craft\helpers\App::env('CONTACT_FROM_EMAIL');

            // Override the transport adapter class
            $settings-&gt;transportType = craft\mail\transportadapters\Smtp::class;

            // Override the transport adapter settings
            $settings-&gt;transportSettings = [

                'host' =&gt; craft\helpers\App::env('SMTP_HOST'),

                'port' =&gt; craft\helpers\App::env('SMTP_PORT'),

                'useAuthentication' =&gt; true,

                'username' =&gt; craft\helpers\App::env('SMTP_USERNAME'),

                'password' =&gt; craft\helpers\App::env('SMTP_PASSWORD')

            ];


            
            // Create a Mailer component config with these settings
            $config = craft\helpers\App::mailerConfig($settings);

            // Instantiate and return it
            return Craft::createObject($config);

        }
    ]
   
];
</code></pre>

CC BY-SA 4.0
2022-07-30T20:53:52.463Z
2022-07-30T20:53:52.463Z
Alex
<p>I have this custom config in config/app.php,
but Craft seems to keep using the default Control panel Sendmail config.</p>

<p>It seems this doesn't get merged in the main.php file. (as promised)</p>

<pre><code> return [
 '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 = craft\mail\transportadapters\Smtp::class;

        // Override the transport adapter settings
        $settings-&gt;transportSettings = [
            'host' =&gt; 'xx',
            'port' =&gt; 'xx',
            'useAuthentication' =&gt; true,
            'username' =&gt; 'xx',
            'password' =&gt; 'xx'
        ];

        return craft\helpers\MailerHelper::createMailer($settings);
     },
 ],
</code></pre>

<p>];</p>

CC BY-SA 4.0
2018-09-07T13:36:13.480Z
Check out https://github.com/craftcms/cms/issues/2554#issuecomment-407713430
Brad
<emails><craft3><craft-pro>
Mailer config in Craft 3 not getting merged
# Mailer config in Craft 3 not getting merged

(Asked by: username_0 on 2018-01-02)

I have this custom config in config/app.php,
but Craft seems to keep using the default Control panel Sendmail config.
It seems this doesn't get merged in the main.php file. (as promised)
 return [
 'components' => [
     'mailer' => function() {

        // Get the stored email settings
        $settings = Craft::$app->systemSettings->getEmailSettings();

        // Override the transport adapter class
        $settings->transportType = craft\mail\transportadapters\Smtp::class;

        // Override the transport adapter settings
        $settings->transportSettings = [
            'host' => 'xx',
            'port' => 'xx',
            'useAuthentication' => true,
            'username' => 'xx',
            'password' => 'xx'
        ];

        return craft\helpers\MailerHelper::createMailer($settings);
     },
 ],

];

**Tags:** emails, craft3, craft-pro

> **Comments:**
> **username_1**: Check out https://github.com/craftcms/cms/issues/2554#issuecomment-407713430  

---

## 2 answers

### Answer by username_2 on 2022-07-31. Score: 1

This was addressed in this issue and the MailerHelper::createMailer() method was deprecated as of Craft 3.0.18, App::mailerConfig() should be used instead.
Changelog: https://github.com/craftcms/cms/blob/v3/CHANGELOG.md#3018---2018-07-31
Here's a working version for Craft 3.1.0 and above.
 [
        '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' => 'xx',
                'port' => 'xx',
                'useAuthentication' => true,
                'username' => 'xx',
                'password' => 'xx'
            ];

            $config = App::mailerConfig($settings);
            return Craft::createObject($config);
        }
    ],
];


---

### Answer by username_3 on 2022-07-30. Score: 1

For me what worked is instantiating and returning a Craft object, see below:
Also, you might want to consider abstracting away env variables.
I am using Craft 4
return [

    'id' => App::env('CRAFT_APP_ID') ?: 'CraftCMS',

    'modules' => [

        'my-module' => \modules\Module::class,

    ],

    'components' => [

        'mailer' => function() {



            $settings = craft\helpers\App::mailSettings();

            $settings->fromEmail = craft\helpers\App::env('CONTACT_FROM_EMAIL');

            // Override the transport adapter class
            $settings->transportType = craft\mail\transportadapters\Smtp::class;

            // Override the transport adapter settings
            $settings->transportSettings = [

                'host' => craft\helpers\App::env('SMTP_HOST'),

                'port' => craft\helpers\App::env('SMTP_PORT'),

                'useAuthentication' => true,

                'username' => craft\helpers\App::env('SMTP_USERNAME'),

                'password' => craft\helpers\App::env('SMTP_PASSWORD')

            ];


            
            // Create a Mailer component config with these settings
            $config = craft\helpers\App::mailerConfig($settings);

            // Instantiate and return it
            return Craft::createObject($config);

        }
    ]
   
];


---

