Trying to get Laravel 5 email to work

2019-01-06 09:17发布

I'm trying to send an email to a specified user by typing in the URL, but I'm getting the following error:

Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required

So far I'm just trying to get it to work with Gmail. How can I get this to work?

This is what I have so far: mail.php

<?php
    return [
        'driver' => env('MAIL_DRIVER',' smtp'),
        'host' => env('MAIL_HOST', 'smtp.gmail.com'),
        'port' => env('MAIL_PORT', 587),
        'from' => ['address' =>"MyUsername@gmail.com" , 'name' => "example"],
        'encryption' => 'tls',
        'username' => env('MyUsername@gmail.com'),
        'password' => env('MyPassword'),
        'sendmail' => '/usr/sbin/sendmail -bs',
        'pretend' => false,
    ];

This is what I have in the routes:

Route::get('test', function() {
    Mail::send('Email.test', [], function ($message) {
        $message->to('example@gmail.com', 'HisName')->subject('Welcome!');
    });
});

This is what I have in my controller:

class MailController extends Controller
{
    public function Sending_Email()
    {
        $this->call('GET','Email.test');
        return View('Email.test');
    }
}

And this is what is in my .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=MyUsername@gmail.com
MAIL_PASSWORD=MyPassword

18条回答
地球回转人心会变
2楼-- · 2019-01-06 09:37

If you ever have the same trouble and try everything online and it doesn't work, it is probably the config cache file that is sending the wrong information. You can find it in bootstrap/cache/config.php. Make sure the credentials are right in there. It took me a week to figure that out. I hope this will help someone someday.

查看更多
聊天终结者
3楼-- · 2019-01-06 09:43

None of the above solutions worked for me on localhost. I even allowed access from less secure apps, allowed access through display unlock captcha and set the verify peer and verify peer name to false for SSL.

Eventually, I used the open source SMTP testing solution of MailHog. The steps are as follows:

  1. Download the latest version of MailHog for your OS
  2. Specify the following settings in your .env file

MAIL_DRIVER=smtp

MAIL_HOST=127.0.0.1

MAIL_PORT=1025

MAIL_USERNAME=testuser

MAIL_PASSWORD=testpwd

MAIL_ENCRYPTION=null

MAIL_FROM_ADDRESS=theaddress@youwant.com

  1. Run the downloaded file of MailHog
  2. Send Email
  3. Check sent email by going to localhost:8025
查看更多
Deceive 欺骗
4楼-- · 2019-01-06 09:44

My .env file configuration is like this for laravel 5.1

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example@gmail.com
MAIL_PASSWORD=****************
MAIL_ENCRYPTION=tls

here most important thing is that I created gmail application specific password(16 digit).

I need to reveal one more thing since no luck for any of configuration. That is, whenever I changed .env file need to run this command

php artisan config:cache

And this is most most most important because without this command laravel executes previous settings from it's cache. It's required me more than 10 hours to figure out.

查看更多
疯言疯语
5楼-- · 2019-01-06 09:44

In my case: Restart the server and run php artisan config:clear command.

查看更多
爷、活的狠高调
6楼-- · 2019-01-06 09:45

You should restart the server and run this commands:

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan config:clear
php artisan config:cache

That should work.

查看更多
乱世女痞
7楼-- · 2019-01-06 09:45

I have the same problem. I fixed these errors by restarting my web server.

查看更多
登录 后发表回答