【问题标题】:Laravel 5 Mail::raw works in tinker but doesn't in appLaravel 5 Mail::raw 在修补程序中有效,但在应用程序中无效
【发布时间】:2020-01-13 11:08:05
【问题描述】:

我从 Laravel 发送 SMTP 电子邮件时遇到了一个非常奇怪的问题

Mail::raw('Email body', function ($message) use ($filename) {
            $message->from('sender@email.com', 'Ordini')
              ->to('user@email.com')
              ->subject('subject')
              ->attach(storage_path($filename));
        });

此代码实际上在服务器的 tinker 环境中工作,但在 controller 中执行时却不能。

我已经仔细检查了 .env 和 config/mail.php,一切都很好

服务器邮件是我客户的smtp邮件服务器

我也试过不带任何附件

响应是“预期的响应代码为 250,但得到一个空响应

我不明白为什么它在修补程序中有效,而在应用程序中无效。有什么区别?

感谢您的建议

【问题讨论】:

  • 请将您的代码放在 try-catch 块中,并检查它是否抛出错误。
  • 是的,代码实际上在 try-catch 块中。响应来自 $e->getMessage();谢谢
  • 尝试运行php artisan config:cache
  • 已经试过了。它没有帮助。谢谢
  • 您的 gmail 是否遵循两步验证或不允许第三方不安全的应用程序

标签: laravel email smtp


【解决方案1】:

确保您没有在您的 gmail 上启用两步验证,并且您在您的 google 帐户上允许安全性较低的应用程序 检查所有内容后尝试运行命令

php artisan config:clear
php artisan cache:clear
php artisan view:clear

确保您的 env 文件包含这些信息

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls

然后

代替你的邮件代码试试这样

        Mail::raw('Email body', function ($message) use ($filename) {
            $message->from('sender@email.com', 'Ordini');
            $message->to('user@email.com');
            $message->subject('subject');
            $message->attach(storage_path($filename));
        });

检查 config/mail.php 如果不是在配置文件中进行更改,是否从 env 获取所有内容

【讨论】:

  • 我使用的不是 gmail,而是我客户的本地 smtp 邮件服务器。我试过你的代码,它没有用。在 env e config 中一切正常(它在 tinker btw 中工作)。非常感谢
猜你喜欢
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 2016-08-10
相关资源
最近更新 更多