major697
major697

Reputation: 1862

Laravel 5.5 does not send e-mails

Hello I try send email from my registration script to user (link). I have config file:

config/mail.php

return [

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

    'host' => env('MAIL_HOST', 's44.linuxpl.com'),

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

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

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

    'username' => env('[email protected]'),

    'password' => env('MySecretPassword'),

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

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
    'stream' => [
        'tls' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true,
        ],
    ]
];

And file .env:

MAIL_DRIVER=smtp
MAIL_HOST=s44.linuxpl.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=MySecretPassword
MAIL_ENCRYPTION=tls

Script to send mail from RegisterController.php

protected function create(array $data)
{

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);

    $verifyUser = VerifyUser::create([
        'user_id' => $user->id,
        'token' => str_random(40)
    ]);

    $sendMail = Mail::to($user->email)->send(new VerifyMail($user));

    return $user;
}  

Class VerifyMail

class VerifyMail extends Mailable
{
    use Queueable, SerializesModels;
    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function build()
    {
        return $this->view('emails.verifyUser');
    }
}

And file to send verifyUser.blade.php

<h2>Welcome to the site {{$user['name']}}</h2>

I don't know what is wrong in this configuration, because I can log in correctly to the mailbox, so the email address and password are correct. Additionally, laravel does not return any errors, the script itself is executed correctly.

Upvotes: 0

Views: 2118

Answers (3)

Anil Kumar Sahu
Anil Kumar Sahu

Reputation: 577

just restore your config/mail.php to default one and try to set the below values in your laravel .env file directly

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=abc@123

May wish this will help you Thanks

Upvotes: 0

major697
major697

Reputation: 1862

I found solution. The problem is with file .env. The file was loaded with previous settings. It was enough to clean the cache files.

php artisan config:cache
php artisan config:clear
php artisan cache:clear

Here it is well described Trying to get Laravel 5 email to work

Upvotes: 0

Jeffrey
Jeffrey

Reputation: 473

Restore your mail.php file to its defaults:
Replace:

'username' => env('[email protected]'),
'password' => env('MySecretPassword'),

with

'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

You misunderstood how the env($key, $default = null) method works. The first argument it takes is the key of the environment variable (eg. 'MAIL_USERNAME'), and the second argument is the default value (which can be optional).

Store your mail credentials in the .env file only, and never within your config files' env() calls.

Laravel Docs: Configuration

Upvotes: 1

Related Questions