In this tutorial we will see laravel 5 send mail example and see how can we send html email in laravel by using html template.
We will be using our own email (Gmail) in my case. So make sure you have enabled pop or SMTP in your mail service. If not you can search for google regarding how to enable SMTP or POP in gmail.
Lets get started.
What are we going to do ?
- We will configure our SMTP details in our
.env
file of laravel application. - Setup controllers and routes.
- Create view to use it as html template.
- Write a mail function to send email to see a laravel send mail example.
Step 1 :-
Open your .env from the root folder of your laravel directory and scroll to the bottom and configure them by looking at the settings below :-
1 2 3 4 5 6 |
MAIL_DRIVER=smtp MAIL_HOST=smtp.googlemail.com MAIL_PORT=587 MAIL_USERNAME=your gmail id MAIL_PASSWORD=your gmail password MAIL_ENCRYPTION=tls |
If you are in production level and working on a live site then you might have to go laravelApp/config/mail.php
and setup your details there.
Step 2:-
Go to your laravel app/Http/Controllers
and create new file and name it as AdminController or you can use the commad line.
– Just go to CMD navigate yo your laravel and type php artisan make:controller AdminController.
Now for routes. Open your your routes file by going to routes/web.php
and paste these lines below or you can make your own routes as well :-
1 2 |
Route::get('test-email-form','AdminController@index'); Route::post('send-html-email','AdminController@sendEmail')->name('send.mail'); |
Step 3 :-
Create new file inside resources/views
folder called email_form.blade.php
and create new folder inside resources/views
called mail and inside mail folder create new file called contact.blade.php
. We are going to paste an email template code inside it for an beautiful and elegant email message.
Paste this whole code inside the contact.blade.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<table border="0"> <tr style="background: skyblue;"> <td colspan="2"> <center><img src="mail_icon.png" width="100" width="100" style="margin-top: 60px;"></center> <br> <center><h1>Welcome {{ $name }}</h1></center> </td> </tr> <tr> <td colspan="2"> You are receving this message because you read our tutorial at <a href="#">codeinhouse.com</a> and tried our article tutorial to send an HTML email With Laravel. If you think It helped you either way then please feel free and help us hand and LIKE or Add us on Google. <a href="https://www.facebook.com/codeinhouse">Facebook</a> <br> <a href="https://www.facebook.com/codeinhouse">Twitter</a> </td> </tr> </table> |
Things you have to notice while creating HTML templates for email are :-
- You have to make everything in tabular format using <table> tags.
- Tags like div wont render in email
- Best to use inline CSS inside.
Now, the mail function inside the AdminController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; class AdminController extends Controller { public function index() { return view('email_form'); } public function sendEmail(Request $request) { $data = [ 'name'=> $request->full_name, 'email'=> $request->email, 'body_message'=> $request->message, ]; Mail::send('emails.contact',$data, function($message) use ($data){ $message->from($data['email']); $message->to('receiptentemail@gmail.com'); $message->subject('Test Subject'); session::flash('success','Message Sent'); return redirect()->back(); }); } } |
This is it, You have successfully sent email with laravel HTML email template. If you want to design up more you can play with HTML and CSS inside contact.blade.php
and make own elegant and beautiful.
Leave a Reply