php - 在 Laravel 5 中发送电子邮件的最佳方式是什么?

标签 php email laravel laravel-4 laravel-5

我在 Laravel 5 应用程序中发送电子邮件时遇到问题。


这是我的邮件功能

电子邮件 = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97e3f2e4e3d7f0faf6fefbb9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>

Mail::send('emails.activation', array(

  'username'=>$user->username,
  'name'=>$user->name,
  'code'=>$user->code,
  'email'=>$user->email

  ),
function($message){
  $message->from(env('MAIL_USERNAME'),'Site');
  $message->to('email', 'name' )->subject('Site Activation ');
});

我不断得到

enter image description here


更新

.env中的邮件配置文件

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fcb1bdb5b0a3a9afb9aeb2bdb1b9c198939293888e998c9085bc8f958899d29f9391" rel="noreferrer noopener nofollow">[email protected]</a>
MAIL_PASSWORD=***********

新错误

enter image description here


我对正在发生的事情感到非常好奇。 我做错了什么?

最佳答案

这意味着电子邮件地址字段(例如发件人、发件人或回复字段)存在错误。

Swift Mailer 严格遵循 RFC 标准,以避免电子邮件被垃圾邮件检查工具捕获。

<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e195849295a1868c80888dcf828e8c" rel="noreferrer noopener nofollow">[email protected]</a>看起来不像普通的电子邮件地址,甚至不存在。

尝试使用不同的电子邮件地址示例发送给我

Mail::send('emails.activation', array(

  'username'=>$user->username,
  'name'=>$user->name,
  'code'=>$user->code,
  'email'=>$user->email

  ),
function($message){
  $message->from('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03706a7766466e626a6f6267677166707043676c6e626a6d2d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>'),'Site');
  $message->to('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="224354434e4b46674f434b4e4346465047515162464d4f434b4c0c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>', 'name' )->subject('Site Activation ');

});

此外,如果您希望使用 Gmail 作为 SMTP 服务器,请执行以下操作:

'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="720b1d07005f171f131b1e32151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>',
'password' => 'your-password',

已编辑

注意:MAIL_USERNAME一定是你的@gmail.com电子邮件地址,例如[email protected]否则您将收到无法与主机 smtp.gmail.com 建立连接错误

如果无法解决此异常,请打开app\Exceptions\handler.php来处理该异常

在渲染方法中添加此内容:

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {


        if ($e instanceof Swift_RfcComplianceException){
            //redirect to form
            //You can also delete the user account here if already created or do other stuffs
            return redirect($request->fullUrl())->with('error',"We have issue sending you an email");

        }


        .......
        
    }

注意:记得添加use Swift_RfcComplianceException;在 handler.php 的顶部

关于php - 在 Laravel 5 中发送电子邮件的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397036/

相关文章:

android - phonegap google play 和邮件链接

php - Laravel Fluent 查询根据行是否存在更新或插入

php - 在 laravel 5.2 的分页链接中添加了随机字符

php - Twig,PHP 计算来自 MySql 数据库的每日客户端事件的数量

php - 透明背景与 PHP 中的 GD

php - public_html 上面图片的存储和读取

android - 如何从 Android 模拟器替换/卸载电子邮件应用程序

php - 如何禁用 fsockopen 中的 ssl 检查? (PHP 5.6)

android - 如何将 Android 选择器列表中的应用程序列为电子邮件客户端?

laravel - 如何从迁移创建 Laravel 模型?