Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Send Email In Laravel Through Email Sending  Service Providers

June 1, 2017

5 Min Read
Reading Time: 5 minutes

One of the most important reasons why Laravel is a popular PHP development framework is the ease with which developers could implement routine functionalities such as authentication, sessions, routing, and caching.

send email in laravel

To facilitate developers in implementing email related functionalities, the framework provides a specific Laravel mail send function which allows developers to send email through a local or cloud-based service.

In this regard, it is important to understand that using an email service provider is a far better option than manually sending the emails. In this article, I will demonstrate how to use Laravel to send emails with the native Mailable function. I will also touch upon how to setup third party email service providers’ API to enhance the email function in Laravel.

If you are new to Laravel you can take a look at getting started with Laravel 5.4, Introductions to Routing, Controllers, Models and Views.

Prevent Your Laravel Applications From Being Hacked

Deploy Your Laravel Apps With Us & Experience The Solidified Security Features

Laravel Installation

To quickly setup a Laravel application, sign up at Cloudways for free. Login to your account and create a new server. Fill in the server and the application detail and select Laravel as your application. That’s it, you just installed a new application within a few clicks on the Cloudways platform.

Creating Mailable

First of all, let’s setup the basic Laravel mail function through PHP artisan. Use the following command in the terminal:

php artisan make:mail <name of mailable>

Now that mailable has been successfully created, go to app/Mail and open your mailable file. The default code for the class would look like this:

namespace App\Mail;                                                                                                                                              

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Contracts\Queue\ShouldQueue;


class mailme extends Mailable


  use Queueable, SerializesModels;



   * Create a new message instance.


   * @return void


  public function __construct()






   * Build the message.


   * @return $this


  public function build()


      return $this->view('');



Now add the following code in the build function:

public function build()


$address = '';

      $name = 'Saquib Rizwan';

      $subject = 'Laravel Email';

      return $this->view('emails.mailme')

      ->from($address, $name)



Next, I will create the email template.

Creating the Laravel Email Template

Now go to resources/views and create a new folder with the name emails.This folder will contain the Laravel email template. Create a new file mailme.blade.php and create the email template using HTML tags.

<h1>Laravel Email</h1>

<p>The email has been send</p>

Configuring the Routes

I will now configure the routes which will trigger the email. Go to routes/web.php and paste in the following code:


use App\Mail\mailme;

Route::get('/', function () {

   Mail::to('')->send(new mailme);

return view('emails.mailme');


Now that the route is ready, I will look into the configurations for different email services providers for sending the email.

Configurations for Gmail

First of all, login to your gmail account. Under My account > Sign In And Security > Sign In to Google, enable two-step verification, and then generate app password,

Now use the app password in .env file with the following configurations.






Now go to Cloudways platform and launch your application with the help of the free staging domain.

And, your email will be sent to the email address which you added to the routes.

Configurations for Mailtrap

First of all, go to Signup and create your account. Once you have logged in, you will see your dashboard.

Click on inbox and get your username and password.

Now use these credentials in .env file with the following configurations.




MAIL_USERNAME= your.mailtrap.user

MAIL_PASSWORD= your.mailtrap.pass


Now go to the Cloudways platform and launch your application. Your email will be sent to your mailtrap inbox.

Configuration for Mailgun

Go to and signup and create your account. After the login, you will be redirected to your dashboard.

I will use the Domain and API Key in .env file and add the following lines:



MAILGUN_SECRET= your_api_key

Now go to the routes/web.php and add the following code.

require '../vendor/autoload.php';

use Mailgun\Mailgun;

$mgClient = new Mailgun('your.api.key');

$domain = "";

# Make the call to the client.

$result = $mgClient->sendMessage("$domain",

         array('from'    => 'Mailgun Sandbox <>',

               'to'      => 'Saquib <>',

               'subject' => 'Hello Saquib',

               'text'    => 'Congratulations Saquib, you just sent an email with Mailgun!  You are truly awesome! '));

Finally, go to the root of your application in the terminal and execute the following command:

composer require mailgun/mailgun-php php-http/guzzle6-adapter php-http/message

That’s it. Go to the Cloudways platform and launch your application using the free staging domain and your email will be sent to your given email address.


In this article, I discussed how you can easily implement email related capability in your Laravel application. If you need help with the code or would like to add to the discussion, do leave a comment below.

Note: To Host your Laravel application on Digital Ocean server FREE for 2 months, Signup at Cloudways and Use Coupon Code “PHP15”.


Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch Laravel apps with 1-Click Install. Painless Server Management

Deploy your apps on optimized Laravel hosting servers.

Saquib Rizwan

Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

BFCM 2019