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_DRIVER = smtp
MAIL_HOST = smtp.gmail.com
MAIL_PORT = 587
MAIL_USERNAME = your-gmail-username
MAIL_PASSWORD = your-application-password
MAIL_ENCRYPTION = tls

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

<?php

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('abc@gmail.com', 'CoderMen')->subject
            ('Laravel Basic Testing Mail');
         $message->from('xyz@gmail.com','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('abc@gmail.com', 'CoderMen')->subject
            ('Laravel HTML Testing Mail');
         $message->from('xyz@gmail.com','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('abc@gmail.com', 'CoderMen')->subject
            ('Laravel Testing Mail with Attachment');
         $message->attach('C:\laravel-master\laravel\public\uploads\image.png');
         $message->attach('C:\laravel-master\laravel\public\uploads\test.txt');
         $message->from('xyz@gmail.com','Rohan Modi');
      });
      echo "Email Sent with attachment. Check your inbox.";
   }
}

 

Step:5 

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>

 

Step:6

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

Route::get('sendbasicemail','MailController@basic_email');
Route::get('sendhtmlemail','MailController@html_email');
Route::get('sendattachmentemail','MailController@attachment_email');

 

Step:7

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

http://localhost:8000/sendbasicemail

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

Step:8

If you enter Flowing URL

http://localhost:8000/sendhtmlemail

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

 

Step:9

This is URL will be sent an attachment mail.

http://localhost:8000/sendattachmentemail

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

    brijpalsharma99@gmail.com, 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.