繁体   English   中英

如何通过Laravel发送验证电子邮件

[英]How to Send Verification Email through Laravel

我正在尝试向新用户发送电子邮件,以便他们可以先验证其电子邮件地址,但是我收到一个异常,该消息表示需要验证,但我的凭据完全正确。

Swift_TransportException

Expected response code 250 but got code "530", with message "530 Authentication required

异常错误详细信息

.Env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.elasticemail.com
MAIL_PORT=2525
MAIL_USERNAME=aniket.************
MAIL_PASSWORD=b6762146-*******-*******-******
MAIL_ENCRYPTION=tls


Mail.php

 'driver' => env('MAIL_DRIVER', 'smtp'),

/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/

'host' => env('MAIL_HOST', 'smtp.elasticemail.com'),

/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/

'port' => env('MAIL_PORT', 2525),

/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'support@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/

'encryption' => env('MAIL_ENCRYPTION', 'tls'),

/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/

'username' => env('aniket.************'),

'password' => env('b6762146-********-*********'),

/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/

'sendmail' => '/usr/sbin/sendmail -bs',

这就是我发送邮件的方式

namespace App\Factories;

use App\Models\User;
use App\Repositories\ActivationRepository;
use Illuminate\Mail\Mailer;
use Illuminate\Mail\Message;
public function __construct(ActivationRepository $activationRepo, Mailer $mailer)
{
    $this->activationRepo = $activationRepo;
    $this->mailer = $mailer;
}

public function sendActivationMail($user)
{
    if ($user->activated || !$this->shouldSend($user)) {
        return;
    }

    $token = $this->activationRepo->createActivation($user);

    $link = route('user.activate', $token);
    $message = sprintf('Activate account %s', $link, $link);

    $this->mailer->raw($message, function (Message $m) use ($user) {
        $m->to($user->email)->subject('Activation mail');
    });
}

需要验证=验证失败。

确保在您的.env文件中(或者如果您使用app.mail ), MAIL_USERNAME是电子邮件的发件人,并且MAIL_USERNAMEMAIL_PASSWORD有效。

from addressmail.php应该是一样MAIL_USERNAME

在您的.env中

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=user@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

在你的config / mail.php中

 'from' => ['address' => 'user@gmail.com', 'name' => null],

这样就可以了! :-D

确保在Google Lesser应用上启用此功能

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM