1

I am using Laravel 4.2

The answer given by user3158900 is for Laravel 5.* Any one can help me with version 4.2 ?

I would like to override sendSwiftMessage() protected function with my own function.

sendSwiftMessage() is located in

"vendor/laravel/framework/src/Illuminate/Mail/Mailer.php"

I created a

Lib/Mailer/CustomMailer.php

and Set the folder Lib to autoload in composer (PSR4). I can now call/load my function in my controllers by writing:

new Lib\Mailer\CustomMailer;

This is how my file looks like:

<?php namespace Lib\Mailer;

class CustomMailer extends \Illuminate\Mail\Mailer {

    /**
     * Send a Swift Message instance.
     *
     * @param  \Swift_Message  $message
     * @return void
     */
    protected function sendSwiftMessage($message)
    {
        if (strpos($message->toString(), '[email protected]') == true) {
            Log::info('Not sending mail to [email protected]');
        }
        else
        {
            if ($this->events)
            {
                $this->events->fire('mailer.sending', array($message));
            }

            if ( ! $this->pretending)
            {
                $this->swift->send($message, $this->failedRecipients);
            }
            elseif (isset($this->logger))
            {
                $this->logMessage($message);
            }
        }
    }

}

However, this sendSwiftMessage() function is not used when I send an email with Swiftmailer in my controller by doing EXAMPLE:

Mail::send(xxxx); 

My question: How can I make Swiftmailer/Laravel use my custom sendSwiftMessage() function when I send a message if I don't want to modify all my Controllers that currently use the Mail::send() code

1
  • The answer from user3158900 is for Laravel 5.* Any one can help me with version 4.2 ? Commented Sep 27, 2016 at 19:44

1 Answer 1

1

Think I got this figured out, however I am getting an error but I think that's on you because your custom class is using a property that doesn't exist so here's the solution anyway.

In AppServiceProvider.php in the boot() method, I've added the following:

$this->app->singleton('customMailer', function($app) {
    return new CustomMailer(
        $app['view'], $app['swift.mailer'], $app['events']
    );
});

In app/Lib/Mailer folder, I've added another class for the facade.

namespace App\Lib\Mailer;

use Illuminate\Support\Facades\Facade;

class Mail extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'customMailer';
    }
}

In config/app.php, I've replaced the Mail alias with the following...

'Mail' => App\Lib\Mailer\Mail::class,

And that should be all you need to do.

One other thing, I just noticed you are missing in your namespace the App which explains why you had to add the Lib folder to the autoloader. If you namespace it correctly to keep it inline with PSR-4 by adding the App\ onto the beginning, then you don't need to add anything to your composer.json file to get additional classes loaded.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please show me where the AppServiceProvider.php should be located and the beginning of the file, thanks!! I'm trying it right now but still stuck over there
Oh you are using Laravel 4. I'm sorry, this code may not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.