I want to send email using smtp with my laravel 8 project. I have an example project in which this works. Using this configuration in my current project is unsuccessful.
I am unable to use the example 1 one 1 because the folderstructure is different. My example uses the default laravel folder structure, my current project has a modular layout in which each folder represents an entity that has its own controllers, models etc. I have been on it for a while and would appreciate outside advice in order to get it working.
The 'allow less secure apps' option is enabled in gmail.
The content of my test_mail.blade.php file is just some basic html.
Mail function is called from inside update function for convenience. Frontend::email.test_mail , this points to a view that is in the same entity folder. The example uses: /folder/subfolder/view, the rest of it is the same as the example.
$data = array("name"=>"testname", "employee_nr"=>"010" );
$subject = 'test_message';
$to_email = 'realtargetaddress@gmail.com';
Mail::send('Frontend::email.test_mail',$data, function($message) use( $to_email,$subject)
{
$message->to($to_email)->subject($subject);
$message->from($to_email,$subject);
});
configuration of .env file. (exact copy from example)
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465 # 587 for tls - 456 for ssl
MAIL_FROM_ADDRESS=some_address@gmail.com
MAIL_USERNAME=some_address@gmail.com
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=ssl # tls or ssl
MAIL_FROM_NAME="${APP_NAME}"
config/mail.php (also a exact copy from my example)
'driver' => env('MAIL_MAILER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'address' => env('MAIL_FROM_ADDRESS',
'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
Mail.php extends Facade This class is in the Illuminate\Support\Facades namespace. My example uses mail.manager in the getFacadeAccessor() method, which fails in my current project. Using mailer instead of mail.manager does not produce errors but also does not work.
console commands that used after making changes to .env
- php artisan config:clear
- php artisan config:cache
- php artisan cache:clear
edit The error that this gives me is: Unable to resolve NULL driver for [Illuminate\Mail\TransportManager]. I searched with that but did not find a fix.
solution I was adviced to add the following code to mail.php, make sure to add it on top and to run the console commands. After i did this the mail function worked for me.
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
],