How to send mail example in Laravel 6

  • 30-03-2022
  • 1133
  • Laravel 6
  • Haresh Chauhan

Laravel Provides mail sending system, So I will tell you in this how-to setup with mail. I will give you instructions step by step mail sending example. In this post, we can easily send emails without too many hassles with your requirements. Laravel provides easy send link mail template, Attachment, CC, BCC, and many more options. we can use the function without any exception. We will discuss in this post. In Laravel email, You can use the Blade syntax and inject data into your templates. In Laravel email, you can also use the core PHP method for send mail and you can also use some email service providers such as MailGun, MailChimp, Sendmail, SMTP, mandrill, mail, Gmail, etc. Laravel provides several ways to send mail. So, now performing this task.

Syntax

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

Step 1 : Setup .env

In this step we require to change in .env file. So,let's change on that file.

.env

MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=webappfix@gmail.com
    MAIL_PASSWORD=mypassword
    MAIL_ENCRYPTION=tls
    

Step 2 : Add Routes

Here,we need to make controller following below command.

php artisan make:controller MailController

routes/web.php

<?php
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
        Route::get('sendbasicemail','MailController@basic_email');
        Route::get('sendhtmlemail','MailController@html_email');
        Route::get('sendattachmentemail','MailController@attachment_email');
    
    

Step 3 : Put code into MailController

In this step we need to copy MailController file code and put into your project file.

app/Http/Controllers/MailController.php

<?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'=>"Dharmesh Chauhan");
       
          Mail::send(['text'=>'mail'], $data, function($message) {
             $message->to('abc@gmail.com', 'webappfix')->subject('Laravel Basic Testing Mail');
             $message->from('xyz@gmail.com','Dharmesh Chauhan');
          });
          echo "Basic Email Sent. Check your inbox.";
       }
       public function html_email() {
          $data = array('name'=>"Dharmesh Chauhan");
          Mail::send('mail', $data, function($message) {
             $message->to('abc@gmail.com', 'webappfix')->subject('Laravel HTML Testing Mail');
             $message->from('xyz@gmail.com','Dharmesh Chauhan');
          });
          echo "HTML Email Sent. Check your inbox.";
       }
       public function attachment_email() {
          $data = array('name'=>"Dharmesh Chauhan");
          Mail::send('mail', $data, function($message) {
             $message->to('abc@gmail.com', 'webappfix')->subject('Laravel Testing Mail with Attachment');
             $message->attach('C:\laravel\storage\uploads\image.png');
             $message->attach('C:\laravel\storage\uploads\test.txt');
             $message->from('xyz@gmail.com','Dharmesh Chauhan');
          });
          echo "Email Sent with attachment. Check your inbox.";
       }
    }
    

Step 4 : Create Blade file

In the last step we require to create blade file. Copy this file and put code on your file and save it.

resources/views/mail.blade.php

<!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Mail Template - webappfix.com</title>
        </head>
        <body>
            <h1>Hi, {{ $name }}</h1>
            <p>Sending Mail from Laravel.</p>
        </body>
    </html>
    

Now, We are ready to run example ,So let's run using quick command.

php artisan serve

Open your browser and fire this url one by one and check your mail inbox.

Run the following URL to test basic email.

localhost:8000/sendbasicemail

Run the following URL to test the HTML email.

localhost:8000/sendhtmlemail

Run the following URL to test the HTML email with attachment.

localhost:8000/sendattachmentemail

If you want to use gmail account into .env file then you must need to set two configuration into gmail account.

Configuration 1

Open you gmail account and following this direction Account > Security > Less secure app access If Less secure app access is OFF Switch to ON.

Configuration 2

Follow this link and click the Continue button.

https://accounts.google.com/DisplayUnlockCaptcha

I hope it can helpful for you.

Thanks...


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :