Search code examples
emaillaravelsmtplaravel-5.1swiftmailer

Laravel sending E-Mail does not work


I am trying to sent emails with laravel but somehow it does not work I get following message:

Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 220 but got code "421", with message "421 Cannot connect to SMTP server 52.20.34.166 (52.20.34.166:587), connect error 10060 "

my .env file looks like this:

APP_ENV=local
APP_DEBUG=true
APP_KEY=rFhHPTfEg4nxYEeqVzoQWX70a0AA5uw3

DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=123456789
MAIL_ENCRYPTION=tls

my mail.php looks like this:

<?php

return [

    'driver' => env('MAIL_DRIVER', 'mail'),
    'host' => env('MAIL_HOST', ''),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => '[email protected]', 'name' => 'test'],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,

];

And inside of my Controlller I called it like this:

    # Send Mail
    Mail::send('emails.email', $data, function($message) use ($data)
    {
        $message->from($data['email'] , $data['title']);
        $message->to('[email protected]', 'my name')->subject('contact request');
    });

I cannot find the mistake in my code. Thanks for help.


Solution

  • Other thing I notices that mailtrap gives you specific username and password. Did you try to use them instead of [email protected];123456789 ?