Laravel-Sending Email

This post describes how to send the email by Laravel application.

Laravel uses SwiftMailer to send emails. Laravel using this library to operate email send.  we will simply send emails while not too several hassles. we can easily send Email and email templates using this SwiftMailer.

Step 1:

We will currently send AN email from Gmail account and for that, you just have to be compelled to piece your Gmail account in Laravel setting file. first, you need to configure your Gmail account in Laravel environment file - .env file.

Second, you need to verification in your Gmail account.

Edit ".env" File

MAIL_USERNAME = your-gmail-username
MAIL_PASSWORD = your-application-password

Step 2

After changes in the .env file make sure to clear the cache of your Laravel application and restart your Laravel server.

php artisan config:cache

Step 3

Create a New controller MailController by executing this following command.

php artisan make:controller MailController 

Step 4

Edit MailController


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class MailController extends Controller {
   public function basic_email(){
      $data = array('name'=>"Rohan Modi");
      Mail::send(['text'=>'mail'], $data, function($message) {
         $message->to('', 'CoderMen')->subject
            ('Laravel Basic Testing Mail');
         $message->from('','Rohan Modi');
      echo "Basic Email Sent. Check your inbox.";
   public function html_email(){
      $data = array('name'=>"Rohan Modi");
      Mail::send('mail', $data, function($message) {
         $message->to('', 'CoderMen')->subject
            ('Laravel HTML Testing Mail');
         $message->from('','Rohan Modi');
      echo "HTML Email Sent. Check your inbox.";
   public function attachment_email(){
      $data = array('name'=>"Rohan Modi");
      Mail::send('mail', $data, function($message) {
         $message->to('', 'CoderMen')->subject
            ('Laravel Testing Mail with Attachment');
         $message->from('','Rohan Modi');
      echo "Email Sent with attachment. Check your inbox.";



Create a new view file resources/views/mail.blade.php and add this following code.

<h1>Hi, {{ $name }}</h1>
<p>Sending Mail from Laravel Application.</p>



Now Add some Routes in app/Http/routes.php.




Now times to test our code. so enter this URL in your Browser


Your output screen will show a message basic Email sent, check your inbox to your Gmail account.


If you enter Flowing URL


You will see another message. HTML Email sent, check your inbox.



This is URL will be sent an attachment mail.


After that enter this URL you will see this message Email Sent with attachment, check your inbox.


In the MailController.php file, the e-mail address within the from technique should be the e-mail address from that you can send email address. Generally, it should be the e-mail address organized on your server.








About Author
  • user profile
    Brijpal Sharma, January 29th, 2018

    Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 Comment

You must be logged in to post a comment.