How to Send Email in Laravel with Example
In this tutorial, We’ll show you How to Send Emails in Laravel with an Example. In this tutorial, we will learn how to send emails in Laravel using SMTP drivers. Laravel default provides an API with a driver for SMTP, SendMail, Sendgrid, Mailgun, Amazon SES, etc. The main benefit of using an SMTP server is to send emails to your local server.
I would like to share a How to Send Emails in Laravel with Example Example. In this tutorial, you will learn how to send Emails in Laravel with an Example. And if you want to see an example of how to send Emails in Laravel functionality, then you are in the right place. In this tutorial, we create a basic send mail using the Laravel mail function.
We can also call the following functions :
$message->subject('Welcome to the Devnote tutorial!');
$message->from('devnote@example.com', 'Devnote');
$message->to('devnote@example.com', 'Devnote');
$message->cc('devnote@example.com', 'Devnote');
$message->bcc('devnote@example.com', 'Devnote');
$message->replyTo('devnote@example.com', 'Devnote');
$message->attach('path/to/devnote.txt');
$message->embed('path/to/devnote.png');
Syntax
Mail::send(['text'=>'text.view'], $data, $callback);
Install Laravel
We need to Download a fresh Laravel setup. Using the below command to download Laravel.
composer create-project --prefer-dist laravel/laravel blog
.env file SMTP configuration
We will set SMTP credentials in the .env file.
# .env MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=gmail_username MAIL_PASSWORD=gmail_password MAIL_ENCRYPTION=tls
Create a controller
We need to create a new controller MailController.
php artisan make:controller MailController
# app/Http/Controllers/MailController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; class MailController extends Controller { public function simple_email() { $data = array('name' => "Joi die"); Mail::send(['text' => 'mail'], $data, function($message) { $message->to('devnote16@gmail.com' , 'Devnote Tutorial')->subject('Laravel Simple Mail Testing'); $message->from('devnote@gmail.com' , 'Joi die'); }); echo "Great! Simple mail successfully send!"; } public function html_email() { $data = array('name' => "Joi die"); Mail::send('mail', $data, function($message){ $message->to('devnote16@gmail.com', 'Devnote Tutorial')->subject('Laravel HTML Mail Testing'); $message->from('devnote@gmail.com' ,'Joi die'); }); echo "Great! HTML mail successfully send!"; } public function attach_email () { $data = array('name' => "Joi die"); Mail::send('mail', $data, function($message){ $message->to('devnote16@gmail.com', 'Devnote Tutorial')->subject('Laravel Attachment Mail Testing' ); $message->attach('D:\blog\public\logo\devnote.png') $message->attach('D:\blog\public\text\devnote.txt') $message->from('devnote@gmail.com', 'Joi die'); }); echo "Great! Attachment mail successfully send!"; } }
View file
Copy the below code and past mail.blade.php file:
resources/views/mail.blade.php
<h1>Hi, {{ $name }}</h1>
<p>Send Email from Laravel.</p>
Route define
We need to define a route for the web.php file inside the routes folder.
# routes/web.php Route::get('simple_email','MailController@simple_email'); Route::get('html_email','MailController@html_email'); Route::get('attachment_email','MailController@attach_email');
Visit the following URL
http://localhost:8000/simple_email Output : Great! Simple mail successfully send!
http://localhost:8000/html_email Output : Great! HTML mail successfully send!
http://localhost:8000/attachment_email Output : Great! Attachment mail successfully send!