Tuesday, June 7, 2016

How to send Email with Laravel? | Sending Email with Laravel | How to setup email configuration and send email in Laravel?




sending mail with laravel


Today's tutorial about Sending Email with Laravel. Email sending is very much useful for an application. Marketing, newsletters, adverts, notification, credentials etc are the main reason you need to send emails to your client. In this tutorial we use gmail as email service provider. So Lets start how you can send email easily with your laravel Project.


1. First open your .env file in your project as here we gonna change some configuration for sending mail.

See the before by default .env setting in MAIL section with fresh laravel installation you get. And here we need to change the setting.
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
And after changing the setting we get this:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=youremailpassword
MAIL_ENCRYPTION=tls
2. Now from your root folder goto Config folder and inside of this open the mail.php as here also we need to change some setting:

Before change of setting with fresh installation we get this in that mail.php file:
<?php

return [

    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => null, 'name' => null],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',

];
And after changing the setting we will get this:
<?php

return [

    'driver' => env('MAIL_DRIVER', 'smtp'),
    'host' => env('MAIL_HOST', 'smtp.gmail.com'),
    'port' => env('MAIL_PORT', 587),
    'from' => ['address' => 'youremail@gmail.com', 'name' =>'YourGmailUserName'],
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username' => env('MAIL_USERNAME'),
    'password' => env('MAIL_PASSWORD'),
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
];
3. Our task in almost done. Now we gonna set the routes.php and make a controller.php to send the email. Let's start with routes

4. In routes.php we need to initialize in which user it will send the user. I have given a demo code you can change with your required code as it's almost same.



Route::get('/sendMail/{id}',[
        'uses' =>'AppController@sendEmail',
        'as' =>'sendMail',
]); 

5. We have to make a view page as we need to send the email with that page only. So here we gonna make a send.blade.php in resources/views folder which we'll send as email.
send.blade.php: 

<html>
   <head> Email Demo</head>
 <body> 
    <p>Thanks for Registering with User Id</p>
 </body>
</html>

6. Now let's a create a RESTful Resource Controller for sending Email. Here we have created sendEmail controller. 



public function sendEmail(Request $request, $id)
        {
            $user = User::findOrFail($id);   
            Mail::send('mail', ['user' => $user], function ($m) use ($user) {
                    $m->from('youremail@gmail.com', 'yourName');
                    $m->to($user->email, $user->name)->subject('Thanks!');
            });
            return redirect('/');
        }


ooh ..don't forget to use namespace use Mail; above of your controller.

That's it. It will find the user with the $id using findOrFail()  then it will send the mail matches with the user information from database.

Thanks for reading the whole article. Hope you can send Email with Laravel now with these easy steps. And if you face any problem don't forget to ping me on comment.




Related Articles

0 comments:

Post a Comment