-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[11.x] Add ability to dynamically build mailers on-demand using Mail::build
#53411
Conversation
$mailer = new Mailer( | ||
$name, | ||
$config['name'] ?? 'ondemand', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use ondemand
as the driver name in the FilesystemManager
:
return $this->resolve('ondemand', is_array($config) ? $config : [ |
@stevebauman This does not seem to work when using |
Will check this, thanks @MrMooky! |
@MrMooky Actually this is intended. You can't queue a mail to be sent with an on-demand mailer. The mailer wouldn't be able to be dynamically built inside of the queued job meant to send the mailable (it wouldn't have the config to do so). The on-demand mailer should be used to send mail immediately, not place on a queue. If you need to queue the sending of the mail, place the creation of the mailer inside of a queued job: namespace App\Jobs;
use App\Models\Mailbox;
use Illuminate\Support\Facades\Mail;
use Illuminate\Contracts\Mail\Mailable;
class SendMailboxEmail implements ShouldQueue
{
public function __construct(
public Mailbox $mailbox,
public Mailable $email,
) {}
public function handle(): void
{
Mail::build([
'transport' => 'smtp',
'host' => $this->mailbox->host,
'port' => $this->mailbox->port,
'encryption' => $this->mailbox->encryption,
'username' => $this->mailbox->username,
'password' => $this->mailbox->password,
'timeout' => 5,
])->send($this->email);
}
} |
@stevebauman Alright, I will give it a try. Thanks for getting back on this. :) |
@stevebauman Still getting |
@MrMooky I'm not able to reproduce that. Can you share reproduction steps? Make sure your mailable doesn't have the I've tested this locally and it works: use App\Models\Mailbox;
use Illuminate\Mail\Mailable;
$mailbox = Mailbox::first();
$email = (new Mailable)
->from($mailbox->email, $mailbox->name)
->to($request->to)
->cc($request->cc)
->bcc($request->bcc)
->html($request->body)
->subject($request->subject);
Mail::build($mailbox->config)->send($email); |
|
Awesome @MrMooky! Glad you got it resolved 🎉 |
@jishadp Post your mailable class |
@stevebauman Its a mistake from my end, Its working perfectly now...So many developers waiting this from India.... |
Awesome, glad you got it sorted @jishadp! |
Mail::fake() is not working when we use Mail::build($config); It is actually trying to send real email from tests. |
@bmerheb Not a bug. Building a mailer in this way doesn't add it into the container. It's a runtime mailer stored in the function scope (not the application container), so you can't assert mail's being sent as you would traditionally. You can instead use mockery: Mail::shouldReceive('build->send')->once()->with(Mailable::class); |
This worked as expected since implementing it into my platform, now I'm getting the following error (haven't changed anything):
When calling this:
|
@MrMooky unrelated to this PR. Please see #53721 (comment) for resolution. |
Description
This PR adds the ability to build on-demand mailers using
Mail::build($config)
.This allows developers to create mailers based on a given configuration instead of being hard-coded in the config files.
This is really useful for circumstances where you may have mail configurations that are stored in your database, or another repository.
We currently have a way to build on-demand Storage disks using
Storage::build
, so I kept the same API.Usage
Let me know your thoughts! No hard feelings on closure. Thanks so much for your time ❤️