4

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);
     },
 ],

];

Thaoms
  • 93
  • 7

2 Answers2

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);

    }
]

];

Alex
  • 111
  • 1
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.

<?php

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

return [ 'components' => [ 'mailer' => 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);
    }
],

];

Ben Croker
  • 7,341
  • 26
  • 55