0

I'm currently using Mailtrap to send test emails. I would like to override Illuminate\Mail\Mailer.php -> send() method. How can I do this in Laravel 12? This is what I have so far:

namespace App\Mail;

use Illuminate\Mail\Mailer as BaseMailer;
use Override;

final class Mailer extends BaseMailer
{
    #[Override]
    public function send($view, array $data = [], $callback = null)
    {
        logger()->channel('mailsender')->info($data);
        
        return parent::send($view, $data, $callback);
    }
}
4
  • If all you need to do is log a line then why not listen to the sent event instead? Commented Sep 17 at 23:14
  • I actually have some event handling for MessageSent. I am trying to do far more additions to Mailer class. Commented Sep 18 at 6:03
  • 2
    Reading the source code here it seems that Laravel resolves a very specific mailer class with no practical way to override which one, you can create custom transports and Mailer seems to be macroable so you can add additional functions via Mailer::macro('name', function () ... but those seem to be your own options. Commented Sep 18 at 6:28
  • This page was helpful Commented Sep 18 at 7:11

0

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.