How To Send Email Using Different Email Service Providers in Laravel
Tags: laravel, webdev, php
Published: 2017-10-31 13:23:52

---
title: How To Send Email Using Different Email Service Providers in Laravel
published: true
description: Learn how to send email in Laravel using different services
tags: Laravel,webdev,php
cover_image: https://thepracticaldev.s3.amazonaws.com/i/0hk4i2t9mnxnlb72uo5c.jpg
---
Laravel has become a popular choice for PHP project development because of the many developer-focused functionalities such as authentication, queues, cron jobs, emailing etc. 

To provide email related functionalities, Laravel provides the Mail function which allows developers to send email through a local or cloud-based service.

The most efficient and effective way of utilizing the email functionalities is to use the email service instead of manually sending the emails. In this article, I will explain how to send emails in Laravel with the  Mailable function. I will also explain in detail how to setup third party email service providers,  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](https://www.cloudways.com/blog/install-laravel-5-4-localhost/), and introductions to [Routing](https://www.cloudways.com/blog/routing-in-laravel-5-4/), [Controllers](https://www.cloudways.com/blog/controllers-middleware-laravel-5-4/), [Models and Views](https://www.cloudways.com/blog/models-views-laravel-5-4/). 

## Install Laravel 

I will start by installing a Laravel application on Cloudways server. The good thing about the process is that it requires just a few clicks to deploy a live application on a cloud server. 

![Deploying Laravel app](http://www.cloudways.com/blog/wp-content/uploads/Laravel_landing-2.gif)

## Create Mailables

To setup the basic Laravel mail function, I will use **php artisan command**. Type the following command in the terminal:  

```php
php artisan make:mail <name of mailable>
```

This will create the mailable class. Next, go to **app/Mail** and open the mailable file. The default code for the class would look like this: 


```php
<?php

                                                                                                                                                   

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('view.name');

   }

}

Now add the following code in the **build **function: 


 public function build()

   {

$address = 'saquibrizwan@xyz.com';

       $name = 'Saquib Rizwan';

       $subject = 'Laravel Email';

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

       ->from($address, $name) 

       ->subject($subject);

 }

```

Next, I will create the email template. 

## Create Email Template

Now go to **resources/views** and create a new folder with the name **emails** This folder will contain the email template. In the folder, 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: 

```php

<?php

use App\Mail\mailme;

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

    Mail::to('saquib.rizwan@xyz.com')->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 &** **Security > Sign In** to Google, enable **two step verification**, and then generate **app password**,

![Gmail Configurre](https://www.cloudways.com/blog/wp-content/uploads/image4-16.png)

Now use the app password in **.env** file with the following configuration:

```

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME= your.email@gmail.com

MAIL_PASSWORD= your.generated.app.password

MAIL_ENCRYPTION=tls
```


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

![Staging Domain](https://www.cloudways.com/blog/wp-content/uploads/image8-11.png)

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

![Send Email](https://www.cloudways.com/blog/wp-content/uploads/image9-9.png)

## Configurations for Mailtrap

First of all go to [Mailtrap.io](https://mailtrap.io/). Signup and create your account. Once you have logged in, you will see your dashboard. 

![Mail trap config](https://www.cloudways.com/blog/wp-content/uploads/image1-17-e1496322037716.png)

Click on inbox and get your username and password. 

![Mail trap config](https://www.cloudways.com/blog/wp-content/uploads/image3-16.png)

Now use these credentials in **.env** file with the following configuration:

```

MAIL_DRIVER="smtp"

MAIL_HOST="mailtrap.io"

MAIL_PORT=2525

MAIL_USERNAME= your.mailtrap.user

MAIL_PASSWORD= your.mailtrap.pass

MAIL_ENCRYPTION=null

```

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

![Mailtrap](https://www.cloudways.com/blog/wp-content/uploads/image7-10.png)

## Configuration for Mailgun 

Go to [Mailgun.com](https://www.mailgun.com/), signup and create your account. After the login, you will redirected to your dashboard.

![Mailgun](https://www.cloudways.com/blog/wp-content/uploads/image2-18.png)

![Mailgun](https://www.cloudways.com/blog/wp-content/uploads/image5-12.png)

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

```
MAIL_DRIVER="mailgun"

MAILGUN_DOMAIN=https://api.mailgun.net/v3/sandbox3bafabb2ef66461ab331b8413425ab23.mailgun.org

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 = "sandbox3bafabb2ef66461ab331b8413425ab23.mailgun.org";

# Make the call to the client.

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

          array('from'    => 'Mailgun Sandbox <postmaster@sandbox3bafabb2ef66461ab331b8413425ab23.mailgun.org>',

                'to'      => 'Saquib <saquib.rizwan@xyz.com>',

                '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 the email will be sent to your given email address. 

![email laravel](https://www.cloudways.com/blog/wp-content/uploads/image6-13.png)

## Conclusion

In this article, I demonstrated how you can easily use Laravel mailable for sending emails with different email service providers. 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](https://platform.cloudways.com/signup) at Cloudways and Use Coupon Code **"PHP15".**