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

时间:2017-07-13 10:24:19

标签: php laravel email laravel-5

我尝试向新用户发送电子邮件,以便他们可以先验证他们的电子邮件地址,但我收到的例外情况说明需要验证但我的凭据完全正确。

Swift_TransportException

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

Exception Error Details

.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');
    });
}

2 个答案:

答案 0 :(得分:3)

需要验证=验证失败..

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

  

from中的address mail.php应与MAIL_USERNAME

相同

答案 1 :(得分:1)

在你的.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 app

上启用此功能