CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Laravel   Laravel 5 Send Mail Example – How to Send Email Using ...

Laravel 5 Send Mail Example – How to Send Email Using Laravel HTML Email Template

August 15, 2017 by SNK

Laravel 5 Send Mail Example

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 ?

  1. We will configure our SMTP details in our .env file of laravel application.
  2. Setup controllers and routes.
  3. Create view to use it as html template.
  4. 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 :-

.env file
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 :-

web.php
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.

Sample Email Example
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 :-

  1. You have to make everything in tabular format using <table> tags.
  2. Tags like div wont render in email
  3. Best to use inline CSS inside.

Now, the mail function inside the AdminController.php

AdminController.php
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.

SHARE ON
Buffer

Enjoyed this article?

Like us on

Laravel laravel 5 send email example laravel html email template laravel send email without view

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.