How to Send Mail in PHP Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to Send Mail in PHP Laravel? Laravel provide us several way to send to email. We can also use this on core PHP method for send the mail and we can also use some email service providers such as sendmail, smtp, mandrill, mailgun, mail, gmail etc. So here we can choice any one and you set configuration. Laravel provide us Mail facade for mail sending that have also several method for send the email.So now in this example here i am going to show you how to send a emails from gmail account example. Now this example is a very simple you can use as your requirement easily. So It is very simple to configuration with gmail account so at first need to open your .env file and add the bellow gmail configuration.

.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

Now you are totally ready to send the mail for test so at first need create test route for email sending.

app/Http/routes.php
Route::get('mail', '[email protected]');

Ok, now need to add mail function in HomeController.php file so add this way :

app/Http/Controllers/HomeController.php
public function mail()
{
    $user = User::find(1)->toArray();
    Mail::send('emails.mailExample', $user, function($message) use ($user) {
        $message->to($user->email);
        $message->subject('E-Mail Example');
    });
    dd('Mail Send Successfully');
}

So now at last need to create a email template file for send mail so let’s create mailExample.blade.php file in emails folder.

resources/views/emails/mailExample.blade.php
Hello {{ $name }}, I am from Codingspoint.com.

Read Also : Laravel Stripe example by using Laravel Cashier from Scratch

Thanks for read. I hope it help you. For more you can follow us onĀ facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →