Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DRN88/85a2ee9d9026914f949bfdf7d70d6243 to your computer and use it in GitHub Desktop.
Save DRN88/85a2ee9d9026914f949bfdf7d70d6243 to your computer and use it in GitHub Desktop.
HowTo Laravel 10 SymfonyMailerHandler Monolog Dependency Injections with Service Container

HowTo Laravel 10 SymfonyMailerHandler Monolog Dependency Injections with Service Container

https://laravel.com/docs/10.x/container#binding-primitives

Having issues with php artisan config:cache, saying it's unserializable? Say no more!

  1. Remove any objects from config/logger.php. See below.
  2. Inject mailer dependencies with AppServiceProvider.php
  3. Note: env() variables does not work in AppServiceProvider.php. Define your variables somewhere, then use config() 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)

config/logging.php

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' => '',
            //],
        ],
        
        ...

.env

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]'

app/Providers/AppServiceProvider.php

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
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment