Skip to content

MailConfigBootstrapper

The MailConfigBootstrapper maps tenant attributes to Laravel’s mail config. It also clears the mailers cached by Laravel’s mail manager so that they’re resolved again using the current tenant’s configuration. When tenancy ends, it restores the original config and clears the cached mailers again.

To enable this bootstrapper, uncomment the MailConfigBootstrapper line in the bootstrappers section of the tenancy config:

config/tenancy.php
'bootstrappers' => [
// Bootstrappers\MailConfigBootstrapper::class,
],

The bootstrapper maps tenant attributes to config using presets: mappings of config keys to tenant attributes, defined per mailer. Out of the box, it only provides a preset for the SMTP mailer:

src/Bootstrappers/MailConfigBootstrapper.php
public static array $mapPresets = [
'smtp' => [
'mail.mailers.smtp.host' => 'smtp_host',
'mail.mailers.smtp.port' => 'smtp_port',
'mail.mailers.smtp.username' => 'smtp_username',
'mail.mailers.smtp.password' => 'smtp_password',
],
];

The preset used is the one for your default mailer, read from config('mail.default') when the bootstrapper is first initialized. So if your default mailer is smtp, a tenant’s smtp_password attribute becomes the mail.mailers.smtp.password config value in that tenant’s context. Only non-null tenant attributes override config. Missing or null attributes leave the existing config value in place.

You’re free to customize the SMTP preset or add presets for other mailers using the $mapPresets static property. For example:

app/Providers/TenancyServiceProvider.php
use Stancl\Tenancy\Bootstrappers\MailConfigBootstrapper;
// In boot()...
MailConfigBootstrapper::$mapPresets['postmark'] = [
'services.postmark.token' => 'postmark_token',
];

To select a preset for a mailer other than the application’s default, set MailConfigBootstrapper::$mailer in your service provider’s boot() method.

Use $credentialsMap to add mappings or override entries from the selected preset. Config keys are on the left, and tenant attribute names are on the right. Nested tenant attributes (e.g. in JSON columns) are supported using dot notation:

app/Providers/TenancyServiceProvider.php
use Stancl\Tenancy\Bootstrappers\MailConfigBootstrapper;
// In boot()...
MailConfigBootstrapper::$credentialsMap = [
'mail.mailers.smtp.password' => 'mail.smtp_password',
'mail.from.address' => 'mail_from_address',
];

Here, the custom password mapping replaces the SMTP preset’s smtp_password mapping. The other SMTP preset entries still apply. As with presets, null values are ignored.

For mail configuration, use this bootstrapper rather than only mapping config with TenantConfigBootstrapper: clearing cached mailers is necessary for mailers to use the current context’s credentials.

Enable the QueueTenancyBootstrapper alongside this bootstrapper and queue mail from the tenant context. The queue bootstrapper initializes tenancy before processing the job, which applies the tenant’s mail configuration and clears cached mailers before sending. This works the same way for queued mailables and queued mail notifications.

You may also want to enable the RootUrlBootstrapper. Queue workers run in CLI context where there’s no request hostname, so URLs in your emails would otherwise point to the central domain. With path identification, you may want to use UrlGeneratorBootstrapper instead.