Laravel 5.7 - 不通过命令行发送电子邮件

问题描述 投票:0回答:3

我正在运行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("[email protected]")
                        ->send(new ShopOrderconfirmation($ec->contact_name, $subject, $ec->shop_name, $ec->order_date));
                }
            }
        }
        else {
            $this->info('Empty.');
        }


    }


}

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

laravel email command command-line-interface
3个回答
0
投票

您需要检查控制台配置,以确保应用程序实例和应用程序的控制台实例之间共享电子邮件配置。

PS:我没有足够的声誉评分来评论


0
投票

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

MAIL_DRIVER=smtp

0
投票

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

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

你也可以尝试tlsMAIL_ENCRYPTION

编辑:

我只是查看了SwiftMailer的AbstractSmtpTransport.php文件,在第445行附近就是这样:

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

这告诉我的是你没有收到邮件服务器的回复。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.