Hi,

Is it possible to configure the SMTP server information and credentials on the settings.php or any configuration file. This helps in automation deployment to different environments.

Comments

Prabhu.shan created an issue. See original summary.

webflo’s picture

Status: Active » Fixed

You can use "Global overrides" to override the swiftmailer settings. Please read the docs on https://www.drupal.org/docs/8/api/configuration-api/configuration-overri...

webflo’s picture

Category: Bug report » Support request
webflo’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ralkeon’s picture

Hi all, I'm trying to override swiftmailer configuration in the settings.php file but with no luck.
I use

$config['swiftmailer.transport']['smtp_host'] = 'smtp.mailtrap.io';
$config['swiftmailer.transport']['smtp_port'] = '465';

but still have values I set from drupal interface... (already cleared cache, nothing happen).
Someone had the same issue?
Thanks for any advise.

uziel.bueno’s picture

You can override swiftmailer configuration with these lines in your settings.php:

// Email Configuration...
$config['swiftmailer.transport']['smtp_host'] = getenv('SMTP_HOST');
$config['swiftmailer.transport']['smtp_credentials']['swiftmailer']['username'] = getenv('SMTP_USERNAME');
$config['swiftmailer.transport']['smtp_credentials']['swiftmailer']['password'] = getenv('SMTP_PASSWORD');
$config['swiftmailer.transport']['smtp_port'] = getenv('SMTP_PORT');
$config['swiftmailer.transport']['smtp_encryption'] = getenv('SMTP_ENCRYPTION');

The only disadvantage of this method is that you won't be able to see the actual configuration values in the Swiftmailer configuration form.

If you want to check what value a configuration key has, you can run this Drush command:

vendor/bin/drush config:get swiftmailer.transport smtp_port
ralkeon’s picture

Uhm... Thanks for the tip but is still not working :(

in the same settings.php file I've this code

$config['smtp.settings']['smtp_host'] = 'smtp.mailtrap.io';

$config['swiftmailer.transport']['smtp_host'] = $config['smtp.settings']['smtp_host'];

but when I run

vendor/bin/drush config:get swiftmailer.transport smtp_host

I still get the configuration I saved by drupal config interface.

uziel.bueno’s picture

Have you tried rebuilding the cache?

vendor/bin/drupal cache:rebuild
ralkeon’s picture

Yep, I tried rebuilding the cache and also set all cache config to false/null, no luck. It's not that great issue because it's working by set config from interface but I always have to check what I'm exporting and pushing on my repo :D

scott.whittaker’s picture

Sorry to necro a closed issue, but this is still an issue.

$config['swiftmailer.transport']['smtp_host'] = 'mailhog';

This seems to be the correct pathing for this setting but adding this to settings.php has no effect.

drush config:set swiftmailer.transport smtp_host mailhog works but using automatic site-specific config would be safer and easier. Do we know why it doesn't work?

Michael Zetterberg fd. Lopez’s picture

Have you tried sending a test e-mail with the overrides in place? You can do so at /admin/config/swiftmailer/test

For me with Drupal 8.9.7 and Swiftmailer 2.0.0-beta1 it works as advertised as per docs at https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-... like webflo said.

Note that values overridden via $config within settings.php will not be viewable from the Drupal administration interface (until #2408549: There is no indication on configuration forms if there are overridden values is fixed, until then you can use Configuration Override Warn or Config Override Inspector) or from inspection via drush (unless you add the --include-overridden flag). The administration interface displays the values stored in configuration so that you can stage changes to other environments that don't have the overrides.

So basically adding --include-overridden to the command is what you are looking for.

$ vendor/bin/drush config-get swiftmailer.transport smtp_host
$ 'swiftmailer.transport:transport': sendmail

gives the "Drupal administration interface" value (I have sendmail set in the admin UI)

Running

$ vendor/bin/drush config-get swiftmailer.transport smtp_host --include-overridden
$ 'swiftmailer.transport:transport': smtp

gives the "overriden" value (I have smtp set in settings.local.php which is conditionally included if it exists in settigs.php)

Sending a test e-mail shows up in mailhog as it should.