Email configuration with Laravel

December 3, 2017

In this article we will see how you can configure Laravel to send email

I am going to use www.mailtrap.io Free account to test the Email

First thing I am going to add SMTP details to .env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=431de9a587e507
MAIL_PASSWORD=74468c2d087d84
MAIL_ENCRYPTION=null

Then you can create controller to test the email

 php artisan make:controller TestEmailController 

Now you can create the send action and add the following code to send test email

public function send(){

      //dd(Config::get('mail'));
      
      $data = array('key'=>"Value");
      Mail::send(['text'=>'mails.welcome'], $data, function($message) {
         $message->to('dev@codekayak.net', 'Developer')->subject
            ('Laravel Basic Testing Mail');
         $message->from('admin@codekayak.net','Admin');
      });
      echo "Basic Email Sent. Check your inbox.";
        
    
    }

Now I am going to create the route for the controller action. In your route\web.php file, add the following line

Route::get('sendemail', 'TestEmailController@send');

Finally you can create the view for the email body
create welcome.blade.php file inside the views\mails folder and put some text in this file

Now you can test the URL http://localhost/user/public/sendemail
You can see the email in emaitrap.io