Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1087

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T04:54:07+00:00 2024-11-25T04:54:07+00:00

Allowing users to send emails with their own SMTP settings in Laravel 9

  • 62k

Various guides exist on how to do this in Laravel, but all the ones I found (this and this) worked only up to Laravel 8. The reason is that in v9 the Swift Mailer was no longer supported in favor of Symfony Mailer. The Laravel docs show how to write your own custom transports, but that's not what we need here. There might be a package that sends emails with custom SMTP params when reading this post, so you might want to take a look first.

What Laravel does behind the scenes

The first thing we want to handle is related to a class called MailManager. It is responsible for configuring and resolving the various transport drivers. We already know that we want to use the SMTP transport, we just need to know how to override the default configuration coming from the .env file. This MailManager has a method for that:

protected function getConfig(string $name) {     return $this->app['config']['mail.driver']         ? $this->app['config']['mail']         : $this->app['config']["mail.mailers.{$name}"]; } 
Enter fullscreen mode Exit fullscreen mode

This is the method that we want to override. Before we do that, we need to know how the MailManager is resolved and used by the framework. In the framework's MailServiceProvider we find this piece of code:

protected function registerIlluminateMailer() {     $this->app->singleton('mail.manager', function ($app) {         return new MailManager($app);     });     $this->app->bind('mailer', function ($app) {         return $app->make('mail.manager')->mailer();     }); } 
Enter fullscreen mode Exit fullscreen mode

This tells us that to work with another mailer instead of the default one, we have to use another instance of a MailManager and get the mailer from it.

Implementing the custom Mailer

So we make this new class called AppExtensionsUserMailManager, place it or call it anything you want, that accepts a configuration when created and overrides the getConfig() method we saw earlier to use that configuration.

namespace AppExtensions;  use IlluminateMailMailManager;  class UserMailManager extends MailManager {     public function __construct($app, private readonly array $customConfig)     {         parent::__construct($app);     }      protected function getConfig(string $name)     {         return $this->customConfig;     } } 
Enter fullscreen mode Exit fullscreen mode

In your AppServiceProvider, or any other provider, we bind an instance of this new MailManager to the container, which will allow us to pass custom SMTP parameters when needed. It could've been a singleton, not sure.

class AppServiceProvider extends ServiceProvider {     public function register(): void     {         $this->app->bind('user.mailer', function ($app, $parameters) {             return (new UserMailManager($app, $parameters))->mailer();         });     }     ... } 
Enter fullscreen mode Exit fullscreen mode

You would utilize our setup so far like this:

/** @var Mailer $mailer */ $mailer = app('user.mailer', $smtpParams); $mailer->send($mailable); 
Enter fullscreen mode Exit fullscreen mode

This does not look all that handy though. Usually, you have your SMTP params in a column in your users table (or another table, doesn't matter). We're going to use that to our advantage, by creating a helper method in the User model.

public function sendMail(Mailable $mailable) {     if ($this->smtp_enabled) {         $smtpParams = [             'transport' => 'smtp',             'host' => $this->smtp_params['smtp_host'] ?? null,             'port' => $this->smtp_params['smtp_port'] ?? null,             'username' => $this->smtp_params['smtp_user'] ?? null,             'password' => $this->smtp_params['smtp_password'] ?? null,             'timeout' => null,             'verify_peer' => false,         ];          /** @var Mailer $mailer */         $mailer = app('creator.mailer', $smtpParams);         $mailer->send($mailable);     } else {         Mail::send($mailable);     } } 
Enter fullscreen mode Exit fullscreen mode

Using the mailer with custom SMTP params

Now, in the other parts of the code where you want to use the custom params, you can swap Mail::send(new TestMail($testVariable)) with $user->sendMail(new TestMail($testVariable)). Handy, right?

If you want to queue the mail instead, I'd suggest you add a new method called queueMail($mailable, ...), and tailor it to your needs. The only difference would probably be using $mailer->queue($mailable); inside the method.

That's it. Leave a thumbs up if this helped you, and thank you for making it this far.

laravelphprefactoringwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.