【问题标题】:Laravel 5.7 - emails not being sent via command lineLaravel 5.7 - 未通过命令行发送电子邮件
【发布时间】:2019-02-26 15:28:35
【问题描述】:

我正在运行一个 5.7 laravel 项目,我可以毫无问题地从控制器发送电子邮件。

但是,当我尝试使用相同的逻辑从命令行启动的命令发送电子邮件时,电子邮件不会发送并且我收到以下错误:

In AbstractSmtpTransport.php line 445:

  Expected response code 220 but got an empty response

这是我的命令代码:

<?php

namespace App\Console\Commands;

use App\Email_confirmation;
use Illuminate\Console\Command;
use App;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use App\Mail\ShopOrderConfirmation;


class sendEmailConfirmations extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:sendEmailConfirmations';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $ec_global = Email_confirmation::where("due_date" ,">", Carbon::now('Europe/Paris'))->get();

        if (!$ec_global->isEmpty()) {
            foreach ($ec_global as $ec) {
                if (App::environment('production')) {
                    $subject = $ec->shop_name . " - Order confirmation";
                    Mail::to($ec->contact_email)
                        ->send(new ShopOrderconfirmation($ec->contact_name, $subject, $ec->shop_name, $ec->order_date));
                }
                elseif (App::environment('development','test')) {
                    $subject = $ec->shop_name . " - Order confirmation - TEST";
                    Mail::to("me@whatever.net")
                        ->send(new ShopOrderconfirmation($ec->contact_name, $subject, $ec->shop_name, $ec->order_date));
                }
            }
        }
        else {
            $this->info('Empty.');
        }


    }


}

项目正在运行 6.0.2 版本的 swiftmailer 包。我在这里找不到行为不同的原因。

【问题讨论】:

    标签: laravel email command command-line-interface


    【解决方案1】:

    请使用 SMTP 通过 Swiftmailer 发送电子邮件

    MAIL_DRIVER=smtp
    

    【讨论】:

      【解决方案2】:

      在您的应用目录中运行env 以检查环境变量是否通过。

      确保 .env 中的 MAIL_DRIVER 属性设置为 smtp

      您也可以尝试tls 换成MAIL_ENCRYPTION

      编辑:

      我刚刚查看了 SwiftMailer 的 AbstractSmtpTransport.php 文件,在第 445 行附近有这样的内容:

      if (!$response) {
          $this->throwException(new Swift_TransportException('Expected response code '.implode('/', $wanted).' but got an empty response'));
      }
      

      这告诉我你没有收到邮件服务器的响应。

      所以我再次建议检查应用目录中env 的输出。如果未反映您的环境变量,请尝试更新它们并运行: php artisan cache:clear php artisan config:clear

      然后再次运行env 以检查是否反映了更新。

      【讨论】:

        猜你喜欢
        • 2019-03-05
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2019-10-18
        • 2011-10-13
        • 2018-03-21
        • 2019-03-09
        • 2023-03-10
        相关资源
        最近更新 更多