How to set the multisite context when sending a mail from the backend


Using the multisite feature, we can differentiate the mail settings across the different sites, which is great:

Now, I’ve set up a schedule in which I loop over users and sends mails in specific cases (membership expiry). Users belong to different subsites and should be mailed using different locales, branding, email settings.

In my Plugin.php file, inside for example registerSchedule(), how can I manually set the multisite context so my user will receive the email from the right sender?

I know we can do something like:

But this is only a part of it. Is there a way to extrapolate from this?

Best regards,


Hey @chocolata

Something like this should work:

$siteId = Site::listEnabled()->where('locale', $newLocale)->first()->id;

Site::withContext($siteId, function () use ($sendTo, $template) {
    Mail::sendTo($sendTo, $template);
1 Like

Hi Sam,

This works perfectly! I managed to send out mails using the subsites’ mail settings this way.
So elegant.

Best regards,