Laravel 不发送电子邮件或未收到邮件

时间:2021-06-07 11:27:04

标签: php laravel swiftmailer

我正在尝试从我的域电子邮件 (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 发送的邮件?

有人知道吗?

请指导我。

1 个答案:

答案 0 :(得分:0)

.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 上接收邮件?