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.
Enabling the bootstrapper
Section titled “Enabling the bootstrapper”To enable this bootstrapper, uncomment the MailConfigBootstrapper line in the bootstrappers section of the tenancy config:
'bootstrappers' => [ // Bootstrappers\MailConfigBootstrapper::class,],Mailer presets
Section titled “Mailer presets”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:
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:
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.
Custom mappings
Section titled “Custom mappings”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:
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.
Queued mail
Section titled “Queued mail”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.