如何在Laravel框架上发送无法发送电子邮件时避免我的Web服务崩溃?

时间:2018-02-06 13:46:11

标签: laravel swiftmailer

我有一个函数用于从API发送所有电子邮件(使用Swiftmailer):

public static function send_email($page, $subject, $email, $email_data) {
    if (env('MAIL_USERNAME') && env('MAIL_PASSWORD')) {
        try {
            $site_url = url('/');
            Mail::queue($page, [
                'email_data' => $email_data,
                'site_url' => $site_url,
            ], function ($message) use ($email, $subject) {
                $message->to($email)->subject($subject);
            });
        } catch (Exception $e) {
            return Helper::get_error_message(123);
        }
        return Helper::get_message(105);
    }else {
        return Helper::get_error_message(123);
    }
}

一切正常,直到客户将他的邮件服务器移动到另一个提供商(没有任何警告!)。

配置正确,一些电子邮件继续运行良好,而其他电子邮件突然停止工作。这是由于新的提供商的政策,它将一些电子邮件视为垃圾邮件。

问题是,当新提供商检测到电子邮件为垃圾邮件时,服务器端会出现一个丑陋的错误,导致Web服务崩溃,然后是移动应用程序..

此行导致问题:

$message->to($email)->subject($subject);

当提供商将电子邮件检测为垃圾邮件时,我可以在Laravel日志中找到此类错误:

[2018-02-06 14:14:47] local.ERROR: Swift_TransportException: Expected response code 250 but got code "550", with message "550 5.2.0 Spam message rejected " in /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383 Stack trace:
#0 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(281): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('550 5.2.0 Spam ...', Array)
#1 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(270): Swift_Transport_AbstractSmtpTransport->executeCommand('\r\n.\r\n', Array, Array)
#2 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(333): Swift_Transport_EsmtpTransport->executeCommand('\r\n.\r\n', Array)
#3 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(428): Swift_Transport_AbstractSmtpTransport->_streamMessage(Object(Swift_Message))
#4 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(444): Swift_Transport_AbstractSmtpTransport->_doMailTransaction(Object(Swift_Message), 'info@jeffrey-se...', Array, Array)
#5 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(176): Swift_Transport_AbstractSmtpTransport->_sendTo(Object(Swift_Message), 'info@jeffrey-se...', Array, Array)
#6 /home/hubert/hubert/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(85): Swift_Transport_AbstractSmtpTransport->send(Object(Swift_Message), Array)
#7 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(385): Swift_Mailer->send(Object(Swift_Message), Array)
#8 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(171): Illuminate\Mail\Mailer->sendSwiftMessage(Object(Swift_Message))
#9 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(276): Illuminate\Mail\Mailer->send('emails.user.wel...', Array, Object(Closure))
#10 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(130): Illuminate\Mail\Mailer->handleQueuedMessage(Object(Illuminate\Queue\Jobs\SyncJob), Array)
#11 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Queue/Jobs/SyncJob.php(44): Illuminate\Queue\Jobs\Job->resolveAndFire(Array)
#12 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php(30): Illuminate\Queue\Jobs\SyncJob->fire()
#13 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(187): Illuminate\Queue\SyncQueue->push('mailer@handleQu...', Array, NULL)
#14 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Illuminate\Mail\Mailer->queue('emails.user.wel...', Array, 'C:32:"SuperClos...')
#15 /home/hubert/hubert/app/Helpers/Helper.php(205): Illuminate\Support\Facades\Facade::__callStatic('queue', Array)
#16 /home/hubert/hubert/app/Http/Controllers/UserapiController.php(667): App\Helpers\Helper::send_email('emails.user.wel...', 'Bienvenue sur J...', 'bruno@email.ch', Object(App\User))
#17 [internal function]: App\Http\Controllers\UserapiController->register(Object(Illuminate\Http\Request))
#18 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(80): call_user_func_array(Array, Array)
#19 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('register', Array)
#20 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\UserapiController), Object(Illuminate\Routing\Route), 'register')
#21 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#22 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#23 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#24 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#25 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#26 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\UserapiController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'register')
#27 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'register')
#28 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Route.php(140): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#29 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Router.php(724): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#30 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#32 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#33 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#35 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#37 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#38 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#39 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#40 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#42 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#43 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#45 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#46 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#47 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#48 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#49 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#50 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#52 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#53 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#54 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#55 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#56 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#57 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#58 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#59 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#60 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#61 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#62 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#63 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#64 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#65 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#66 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#67 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#68 /home/hubert/hubert/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#69 /home/hubert/hubert/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#70 {main}

即使未发送电子邮件,我如何确保网络服务继续有效?

--- --- EDIT

正如所建议的那样,我也试过了:

public static function send_email($page, $subject, $email, $email_data) {
    if (env('MAIL_USERNAME') && env('MAIL_PASSWORD')) {
        try {
            $site_url = url('/');
            Mail::queue($page, [
                'email_data' => $email_data,
                'site_url' => $site_url,
            ], function ($message) use ($email, $subject) {
                try {
                    $message->to($email)->subject($subject);
                } catch (Exception $e) {
                    Log::info('email was not sent!!!');
                }
            });

        } catch (Exception $e) {
            return Helper::get_error_message(123);
        }
        return Helper::get_message(105);
    } else {
        return Helper::get_error_message(123);
    }
}

不幸的是,我仍有同样的问题。

0 个答案:

没有答案