Search code examples
laravel-9

Laravel control connect smtp email


How to check smtp connect for smtp email in laravel9 (event) ?

Laravel6 and 7 : at job workers .. that I basically connect to the mail server only once .. and the job worker runs nonstop .. so after a while the mail server has already timed out, because they haven't communicated for a long time, and sending e-mails has stopped working ..

laravel9: the solution which is below doesn't work can't find the class swift.mailer can you provide me the solution? for laravel9 in the given problem?

example: in version Laravel6,7

class MessageSending
{
 public function __construct()
 {
    //
 }

public function handle($event)
{
    $transport = app('swift.mailer')->getTransport();
    // check if connection is still alive or reconnect
    if (!$transport->ping()) {
        $transport->stop();
        $transport->start();
    }
   }
}

In laravel 9 error ( not find swift.mailer class )


Solution

  • On upgrading your Laravel installation, you should check the documentation for changes. In this case, the important stuff is listed in the section Symfony Mailer:

    One of the largest changes in Laravel 9.x is the transition from SwiftMailer, which is no longer maintained as of December 2021, to Symfony Mailer.

    That means, SwiftMailer (which you try to use with swift.mailer) is no longer installed in your application.


    I doubt that the same transport settings are still available (for example, the start() method on the SmtpTransport class is now private), so you should change the logic of that code. You could for example use Symfony's Messenger to send the mails asynchronously