{"id":1988,"date":"2017-01-07T14:26:07","date_gmt":"2017-01-07T12:26:07","guid":{"rendered":"https:\/\/guven.atbakan.com\/blog\/?p=1988"},"modified":"2024-04-14T00:25:51","modified_gmt":"2024-04-13T21:25:51","slug":"laravel-5-3-change-mail-configuration-dynamically","status":"publish","type":"post","link":"https:\/\/guven.atbakan.com\/blog\/laravel-5-3-change-mail-configuration-dynamically\/","title":{"rendered":"Laravel 5.3 change mail configuration dynamically"},"content":{"rendered":"<p>If you are developing CMS software or a SaaS product, your customers needs to set their mail sending credentials for sending mails from their system. By default, you can set mail configuration to .env file.<\/p>\n<p>But if your customer don&#8217;t have access to .env file and command line to execute artisan config:clear command, they need to change credentials from their panel. Here is an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1989\" src=\"https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304.png\" alt=\"\" width=\"1137\" height=\"404\" srcset=\"https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304.png 1137w, https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304-300x107.png 300w, https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304-768x273.png 768w, https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304-1024x364.png 1024w, https:\/\/guven.atbakan.com\/blog\/wp-content\/uploads\/2017\/01\/Screenshot-from-2017-01-07-143304-500x178.png 500w\" sizes=\"auto, (max-width: 1137px) 100vw, 1137px\" \/><\/p>\n<p>I store these values at database. So, i need to change mail config before sending email. But, Laravel&#8217;s<\/p>\n<pre class=\"lang:default decode:true \">config()-&gt;set('mail',$values);<\/pre>\n<p>doesn&#8217;t work. Because, SwiftMailer instance registers when compiling Service Providers with default parameters which are coming from .env file. So, there is two option for override this instance.<\/p>\n<p>1- Create new Swift_Mailer instance and set it before sending email.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n$swiftMailer = new Swift_Mailer();\r\n\/\/do your configuration with swift mailer\r\nMail::setSwiftMailer($swiftMailer);\r\nMail::to('mail')\r\n-&gt;send();<\/pre>\n<p>But, i do not use this metod. I don&#8217;t want to fight with SwiftMailer&#8217;s configuration. And also, it&#8217;s bad architect. It stand againts Dependency Injection.<\/p>\n<p>2- Override MailService Provider<\/p>\n<p>It has 3 step to set-up.<\/p>\n<p>a) Create AppManagerMailTransportManager. (You can use your namespace.) In this file, we will get mail configuration from database (or wherever you save) and will set to laravel&#8217;s config.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\/**\r\n * @author Guven Atbakan\r\n *\/\r\n\r\nnamespace AppManager;\r\n\r\nuse AppModulesSettingServiceSettingService;\r\nuse IlluminateMailTransportManager;\r\n\r\nclass MailTransportManager extends TransportManager\r\n{\r\n    public function __construct(IlluminateFoundationApplication $app)\r\n    {\r\n        $settings = []; \/\/get your settings from somewhere\r\n\r\n        \/\r\n            $this-&gt;setDefaultDriver('smtp');\r\n            $this-&gt;app['config']['mail'] = [\r\n                'driver' =&gt; 'smtp',\r\n                'host' =&gt; $settings['mailHost'] ?? 'smtp.yandex.net',\r\n                'port' =&gt; $settings['mailPort'] ?? 465,\r\n                'from' =&gt; [\r\n                    'address' =&gt; $settings['mailFromAddress'] ?? '',\r\n                    'name' =&gt; $settings['mailFromName'] ?? '',\r\n                ],\r\n                'encryption' =&gt; 'tls',\r\n                'username' =&gt; $settings['mailUsername'] ?? '-',\r\n                'password' =&gt; $settings['mailPassword'] ?? '-',\r\n                'sendmail' =&gt; $settings['mailSendmailPath'] ?? '\/usr\/sbin\/sendmail -bs',\r\n                'pretend' =&gt; false,\r\n            ];\r\n\r\n            config()-&gt;set('mail', $this-&gt;app['config']['mail']);\r\n            \/\/i'm not sure if its necessary.\r\n    }\r\n}\r\n<\/pre>\n<p>b) Create AppProvidersCustomMailServiceProvider. In this file, we will tell\u00a0 to swift mailer, use our MailTransportManager.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace AppProviders;\r\n\r\nuse AppManagerMailTransportManager;\r\nuse IlluminateMailMailServiceProvider;\r\n\r\nclass CustomMailServiceProvider extends MailServiceProvider\r\n{\r\n    protected function registerSwiftTransport()\r\n    {\r\n        $this-&gt;app['swift.transport'] = $this-&gt;app-&gt;share(function ($app) {\r\n            return new MailTransportManager($app);\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>c) By using config\/app.php, we will disable default MailServiceProvider and register our CustomMailServiceProvider. It&#8217;s easy.<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\/\/IlluminateMailMailServiceProvider::class,\r\nAppProvidersCustomMailServiceProvider::class,<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are developing CMS software or a SaaS product, your customers needs to set their mail sending credentials for sending mails from their system. By default, you can set mail configuration to .env file. But if your customer don&#8217;t have access to .env file and command line to execute artisan config:clear command, they need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[763],"tags":[777,779],"class_list":["post-1988","post","type-post","status-publish","format-standard","hentry","category-development-and-internet","tag-laravel-en","tag-laravel-dynamic-mail-config-en"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/1988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/comments?post=1988"}],"version-history":[{"count":4,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions"}],"predecessor-version":[{"id":1993,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions\/1993"}],"wp:attachment":[{"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/media?parent=1988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/categories?post=1988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guven.atbakan.com\/blog\/wp-json\/wp\/v2\/tags?post=1988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}