Laravel 5 Tutorial : How to Send Email using Gmail SMTP in laravel 5.3

Laravel 5.3 Tutorial for beginners : how to send mail using smtp.gmail.com in laravel 5.3, send email function in Laravel 5.3 apps for beginners

Laravel 5.3 tutorial step by step - Laravel 5.3 can handle email functions, that we can send email using costum SMTP like smtp.gmail.com, Laravel uses free feature-rich library "SwiftMailer" to send emails.

At the previews lessons about Laravel 5.3 we has learned How to Create File Upload in laravel 5.3 and How to use Validation in Laravel 5.3, just learn it before try this one.

Send Email using Gmail SMTP in laravel 5.3

now, this lessons we will create simple apps that we can send email using smtp gmail.com, by following this syntax:

Mail::send(['text'=>'text.view'], $data, $callback);

First, we need to configurate our gmail account to Laravel project, that is on ".ENV" file, costum and configure with your gmail account :

How to Send Email using Gmail SMTP in laravel 5.3

.env file

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

Next, we need to clear our cache and restar Laravel Server by following this command :

Clear cache

php artisan config:cache

Next, we will create new controller to send email with Laravel by following this command :

Create new Controller

php artisan make:controller MailController

next, we will create three function in MailController.php
MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

// we will use Mail namespace
use Mail;

class MailController extends Controller
{
    // first, we create function for send Basics email
    public function basic_email(){
        $data=['name'=>'Harison matondang'];
        Mail::send(['text'=>'mail'], $data, function($message){
            $message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with Basics Email');
            $message->from('nabilaauliaputri29@gmail.com','Nabila');
        });
        echo 'Basics Email was sent!';
    }

    //create new function to send HTML email
    public function html_email(){
      $data=['name'=>'Harison matondang'];
      Mail::send(['text'=>'mail'], $data, function($message){
          $message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with HTML Email');
          $message->from('nabilaauliaputri29@gmail.com','Nabila');
      });
      echo 'HTML Email was sent!';
    }

    //create new function to send mail with attachment Mail
      public function attachment_email(){
        $data=['name'=>'Harison matondang'];
        Mail::send(['text'=>'mail'], $data, function($message){
            $message->to('harisonmatondang@gmail.com','Harison Matondang')->subject('Send Mail from Laravel with HTML Email');
            // add attach here
            // i have a image file on Laravel project
            $message->attach('C:serverhtdocshckrcompublicuploadsharison.jpg');
            $message->attach('C:serverhtdocshckrcompublicuploadssector-code.jpg');
            $message->from('nabilaauliaputri29@gmail.com','Nabila');
        });
        echo 'HTML Email was sent!';
      }
}

Next, create new view, that folder stored on resources/views and rename it with "mail.blade.php"

mail.blade.php

Hallo, {{ $name }}
i send a mail

Next create new routes

web.php

Route::get('/basicemail', 'MailController@basic_email');
Route::get('/htmlemail', 'MailController@html_email');
Route::get('/attachemail', 'MailController@attachment_email');

After finished, try to access your project with browser by following this URL :

http://localhost:8080/basicemail
http://localhost:8080/htmlemail
http://localhost:8080/attachemail

If you got some error like about Security by your gmail account, just setting and Allow less secure applications, by follwing this URL https://myaccount.google.com/security#activity and you will solved your problem.

Video tutorial Send Email with laravel



See you Next Lessons ...

COMMENTS


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,89,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,6,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Laravel 5 Tutorial : How to Send Email using Gmail SMTP in laravel 5.3
Laravel 5 Tutorial : How to Send Email using Gmail SMTP in laravel 5.3
Laravel 5.3 Tutorial for beginners : how to send mail using smtp.gmail.com in laravel 5.3, send email function in Laravel 5.3 apps for beginners
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDVnaMUiM7JI0xQ6Y3r6BEz8i_EeMGhMVCQFUXJuI8_lZzvTbSLFs-LqcE0AFjLmvovXSz4e3s8afXOk6uV0arX6cAdDPZkNO-UXhKmOIuaySeRxXqra98PuCgHbLCLIIq_7GcwFFLlIo/s320/laravel-tutorial-for-beginner.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDVnaMUiM7JI0xQ6Y3r6BEz8i_EeMGhMVCQFUXJuI8_lZzvTbSLFs-LqcE0AFjLmvovXSz4e3s8afXOk6uV0arX6cAdDPZkNO-UXhKmOIuaySeRxXqra98PuCgHbLCLIIq_7GcwFFLlIo/s72-c/laravel-tutorial-for-beginner.png
KODE AJAIB
https://scqq.blogspot.com/2016/09/laravel-5-tutorial-how-to-send-email.html
https://scqq.blogspot.com/
https://scqq.blogspot.com/
https://scqq.blogspot.com/2016/09/laravel-5-tutorial-how-to-send-email.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy