【问题标题】:Laravel not sending email or not received mailLaravel 不发邮件或收不到邮件
【发布时间】:2021-06-07 11:27:04
【问题描述】:

我正在尝试从我的域电子邮件 (cPanel) 发送邮件,但我的 Gmail 帐户中没有收到任何邮件。此外,Laravel 没有显示任何错误。

.env

MAIL_MAILER=smtp
MAIL_HOST=mail.domain.com
MAIL_PORT=465
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=support@domain.com
MAIL_FROM_NAME="${APP_NAME}"

另外,我将implements MustVerifyEmail 添加到User 模型类。

web.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendUserEmailVerification;

Route::get('/send-email', function () {
    Mail::to("myaccount@gmail.com")->send(new SendUserEmailVerification());
    return response("success");
});

SendUserEmailVerification.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendUserEmailVerification extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */

    public function __construct()
    {
        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.user.email_verfy');
    }
}

email_verfy.blade.php

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

当我使用mailtrap.io 时,它会在收件箱中显示邮件。

此外,它正在努力将PHPMailer 用作https://github.com/PHPMailer/PHPMailer,但在 Laravel 上管理电子邮件验证似乎有点困难。

为什么我没有收到 Laravel 发送的邮件?

有人知道吗?

请指导我。

【问题讨论】:

  • 发送电子邮件时出现任何错误
  • 添加 try catch 到 mail::to 看看你有什么异常
  • 试一试,看看你在邮件异常中得到了什么。我认为这将是同样的错误。也不要忘记检查 gmail 垃圾邮件文件夹
  • 检查 gmail 中的垃圾邮件文件夹

标签: php laravel swiftmailer


【解决方案1】:

.ENV

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-gmail-username
MAIL_PASSWORD=your-application-specific-password
MAIL_ENCRYPTION=tls

使用Mail::raw() 函数发送:

\Mail::raw('Hi, welcome user!', function ($message) {
  $message->to(..)
    ->subject(..)
    ->setBody('<h1>Hi, welcome user!</h1>', 'text/html');
});

其他选项是使用Mail::send() 函数:

\Mail::send([], [], function ($message) {
  $message->to('my@email.com')
    ->subject('my subject')
    ->setBody('Hi, welcome user!'); 
});

希望这对你有用,请告诉我!

Tomerikoo 的评论后编辑前的上一个答案

到底出了什么问题?您不想通过Mailtrap 而是通过 Gmail 接收邮件吗?

【讨论】:

  • 抱歉,我明白了。我已经在上面编辑了我的答案,希望这有助于@JSTECH。
  • 我想我不明白你的意思。您想将邮件发送到 Gmail 地址吗?我在回答中发布了两个选项。你测试过这个吗?你读过关于测试和伪装的Laravel docs吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 2013-07-17
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2015-09-16
相关资源
最近更新 更多