AppDividend
Latest Code Tutorials

Laravel 6 Sending Email | How To Send Email In Laravel

22

If you are looking for a solution for How To Send Email In Laravel then this post will answer that question.  We will use Laravel Mailable class to send an email. Laravel provides a clean, simple API over the great SwiftMailer library with drivers for SMTP, Mailgun, SparkPost, Amazon SES, PHP’s mail function, and sendemail, allowing you to quickly get started sending mail through a local or cloud-based service of your choice.

Send Email In Laravel 6

A new feature from Laravel 5.3 is a way to simplify sending an email by creating a “mailable” class that handles setting up your emails.

So we will first define the email configuration inside a .env file and then create a mailable class. While building that mailable class’s instance, we pass the data that needs to be displayed in the mail and then we are good to go.

First, we will start this example by downloading the latest Laravel which is Laravel 6 for this post.

Step 1: Install Laravel 6

composer create-project laravel/laravel emailsend --prefer-dist

After that, we need to define the Email configuration inside the .env file.

Step 2: Email Configuration.

For this example, I am using mailtrap

It is a kind of application, which you can use to test your emails. After you will signup, you can see that it provides us following kinds of details.

Host:	smtp.mailtrap.io
Port:	25 or 465 or 2525
Username:	// some username
Password:	// some password
Auth:	PLAIN, LOGIN and CRAM-MD5
TLS:	Optional

We have to use these details inside the .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password
MAIL_ENCRYPTION=null

If you are using Gmail or your company email address, then please replace your respected credentials here.

Step 3: Create a Mailable class.

Go to a terminal and type the following command.

php artisan make:mail SendMailable

So, it will create this file inside App\Mail\SendMailable.php. Now, this class contains one property, and that is a name that we need to pass when we build this class’s instance.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailable extends Mailable
{
    use Queueable, SerializesModels;
    public $name;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($name)
    {
        $this->name = $name;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.name');
    }
}

Here in this class, I have used the parameterized constructor.

So we have a $name property in our class.

Now, we need to create a blade file inside the views folder. We can use that $name property as a data of the view name.blade.php file. Now, first, form that view.

Step 4: Create a view file.

Inside the views folder, it is better to create a new folder called email and in that folder, make one file called name.blade.php.

<div>
    Hi, This is : {{ $name }}
</div>

Step 5: Define the route to send an email.

Inside routes  >>  web.php file, we need to define the route to send an email.

Route::get('/send/email', 'HomeController@mail');

Now, we need to write the code inside the mail function to send an email.

// HomeController.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

public function mail()
{
   $name = 'Krunal';
   Mail::to('krunal@appdividend.com')->send(new SendMailable($name));
   
   return 'Email was sent';
}

We have included the Mailable class and Mail facade. For this example, we have hardcode the name value, but in a real-time example, we have dynamic data to generate an email.

Step 6: Start the server.

Go to your terminal and hit the following command.

php artisan serve

Go to this URL: http://localhost:8000/send/email

If all the configurations are right then you can see the response like “Email was sent.” So this is how you can send the email. So our How To Send Email In Laravel Tutorial is over.

Recommended Posts

Laravel 6 CRUD Example

Laravel 6 Validation Example

Laravel 6 Export Data in CSV

Laravel 6 Generate PDF

Laravel File Upload Example

22 Comments
  1. TCSET says

    Thanks, Kelsey!

  2. Umair says

    Thanks, A lot man you made my day.

  3. oscam says

    bonjour
    Good share.
    In my opinion the great cccam are those from Fishbone IPTV cloud
    I would like to see more posts like this
    Thanks

  4. Ajay Malhotra says

    Useful Post and working

  5. Reema says

    Hi..
    How did u made the controller that step is not shown.
    Because I am getting error in controller as “/mail not found”.
    Please suggest.

    1. Reza Kazemifar says

      php artisan make:controller HomeController

  6. swati belure says

    i am getting this error
    InvalidArgumentException
    View [emails.demo] not found.

    1. srinath says

      make sure that your file saved in ( resources / views /emails / demo.blade.php )

  7. Ahmed Shahzad says

    Good post nice work

  8. youjan says

    how to put header in the mail? I have tried to send the mail with image of base64 encoded but in gmail the image get the raw html code. Could you please help me to solve this issue?

  9. Ved Prakash N says

    i have given correct credientials .. but then too am getting this error:
    Expected response code 220 but got an empty response

    1. John Nnanna says

      Hello @Ved, I was having same issue; I changed my Port in the .env to 25 and that fixed it

  10. UsbBush says

    Working

  11. Shafeena says

    This is not working,how can I check this works or not,and I can’t get the mail

  12. utpal says

    Expected response code 250 but got code “530”, with message “530 5.7.1 Authentication required

  13. Sora Dale says

    Thank you so much! Worked wonders!
    Working.
    If you encountered and error like “Expected response code 250 but got code “530”, with message “530 5.7.1 Authentication required”
    just go to .env and change the port to 25
    also make sure to restart php artisan serve.

  14. Bouchra says

    Hi,
    It shows ‘Email was sent’, i checked my address email, an there is no mail received.
    Thank you

  15. Bush_AS says

    Hi,
    It shows ‘Email was sent’, i checked my address email, an there is no mail received.
    Thank you

  16. kartik rawal says

    seriously awesome post thanks for this, You solve every query for me. I am a beginner but by your solution i made it, Thanks

  17. Alvin Yanson says

    To anyone having an error InvalidArgumentException View [emails.demo] not found. Change the ’emails’ to ’email’ which is the name of the folder inside the views.

  18. Rajan Panchal says

    Getting Error Undefined variable: name in SendMailable.php file

  19. muhammad says

    thanks.
    you can use https://mailtrap.io/ service for test mail in localhost

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.