https://laravel.com/docs/10.x/container#binding-primitives
Having issues with php artisan config:cache
, saying it's unserializable? Say no more!
- Remove any objects from
config/logger.php
. See below. - Inject mailer dependencies with
AppServiceProvider.php
- Note: env() variables does not work in
AppServiceProvider.php
. Define your variables somewhere, then useconfig()
to access them.
$mailer
and $email
variables are the given class' (defined in when()
) constructor arguments.
# \Monolog\Handler\SymfonyMailerHandler::class
public function __construct($mailer, Email|Closure $email, int|string|Level $level = Level::Error, bool $bubble = true)
return [
#region Custom Variables
'mailMailer' => env('MAIL_MAILER'),
'mailHost' => env('MAIL_HOST'),
'mailPort' => env('MAIL_PORT'),
'mailUsername' => env('MAIL_USERNAME'),
'mailPassword' => env('MAIL_PASSWORD'),
'mailSubject' => env('MAIL_SUBJECT'),
'mailFromAddress' => env('MAIL_FROM_ADDRESS'),
'mailFromName' => env('MAIL_FROM_NAME'),
'mailToAddress1' => env('MAIL_TO_ADDRESS1'),
'mailToAddress2' => env('MAIL_TO_ADDRESS2'),
#endregion
...
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'email'],
'ignore_exceptions' => false,
],
'email' => [
'driver' => 'monolog',
'handler' => SymfonyMailerHandler::class,
'formatter' => HtmlFormatter::class,
//'handler_with' => [
// 'mailer' => '',
// 'email' => '',
//],
],
...
MAIL_MAILER='smtp'
MAIL_HOST='mailserver.mydomain.local'
MAIL_PORT=587
MAIL_SUBJECT='MySubject'
MAIL_ENCRYPTION=null
MAIL_USERNAME='[email protected]'
MAIL_PASSWORD='MyPassword'
MAIL_FROM_ADDRESS='[email protected]'
MAIL_FROM_NAME='MyFromName'
MAIL_TO_ADDRESS1='[email protected]'
MAIL_TO_ADDRESS2='[email protected]'
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
#region Logging to Email
$this->app->when(\Monolog\Handler\SymfonyMailerHandler::class)->needs('$mailer')->give(
new Mailer(Transport::fromDsn(config('logging.mailMailer') . '://' . urlencode(config('logging.mailUsername')) . ':' . urlencode(config('logging.mailPassword')) . '@' . config('logging.mailHost') . ':' . config('logging.mailPort') . '?verify_peer=0'))
);
$this->app->when(\Monolog\Handler\SymfonyMailerHandler::class)->needs('$email')->give(
function () {
$email = new Email();
$email->subject(config('logging.mailSubject'));
$email->from(new Address(config('logging.mailFromAddress'), config('logging.mailFromName')));
$email->addTo(new Address(config('logging.mailToAddress1')));
$email->addTo(new Address(config('logging.mailToAddress2')));
return $email;
}
);
#endregion
}