Library in Laravel for sending emails

Author - Mahesh Kumar
22.01.2019
|
0 Comments
|

Support for or a library in Laravel for sending emails

Laravel uses free feature-rich library SwiftMailer to send emails. It is easy to use. Email templates can be created and loaded in the same way as views.We can use the Blade file syntax for the email view file.

Laravel provides drivers for SMTP, Mailgun, Mandrill, SparkPost, Amazon SES, PHP’s mail function, and sendmail
The API based drivers such as Mailgun and Mandrill are often simpler and faster than SMTP servers.

Guzzle (PHP library) HTTP client

It is a PHP HTTP client which makes it easy to send HTTP requests and integrate with web services(for API calls). Guzzle (PHP library) is required for API drivers. Guzzle can be installed by adding the following line to the composer.json file:

 "guzzlehttp/guzzle": "~5.3|~6.0"

These are the drivers used in laravel to send email:-
1) Mailgun Driver

1. Install Guzzle
2. set the driver option in config/mail.php configuration file to mailgun.
3. Check the config/services.php configuration file contains the following text:

'mailgun' => [
 'domain' => 'your-mailgun-domain',
 'secret' => 'your-mailgun-key',
],

2) Mandrill Driver

1. Install Guzzle
2. Set the driver option in your config/mail.php configuration file to mandrill.
3. Check config/services.php configuration file contains the following options:

'mandrill' => [
 'secret' => 'your-mandrill-key',
],

3) SparkPost Driver
1. Install Guzzle
2. Set the driver option in your config/mail.php configuration file to sparkpost.
3. Check the config/services.php configuration file contains the following options:

'sparkpost' => [
 'secret' => 'your-sparkpost-key',
],

4) SES Driver
1. Install the Amazon AWS SDK for PHP by adding the following line to composer.json file require section:

"aws/aws-sdk-php": "~3.0"

2. Set the driver option in your config/mail.php configuration file to ses.
3. Check the config/services.php configuration file contains the following options:

'ses' => [
 'key' => 'your-ses-key',
 'secret' => 'your-ses-secret',
 'region' => 'ses-region', // e.g. us-east-1
],

5) Smtp Driver

1. Open the (project)/.env file of project.
2. Change the following:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls

3. Set the driver option in your config/mail.php configuration file to smtp

Send Email Using Laravel and SMTP:
Step1:
In the .env file add the smtp server details as follow:

       MAIL_DRIVER=smtp
		MAIL_HOST=smtp.gmail.com
		MAIL_PORT=587
		MAIL_USERNAME=null
		MAIL_PASSWORD=null
		MAIL_ENCRYPTION=tls 

Step 2: Clear cache and restart the laravel server

 php artisan config:cache

Step 3: Change the driver in the config/mail.php

 'driver' => env('MAIL_DRIVER', smtp),

Step 4: Create a view file for the email in resources/view/email
Emailview.php

<html>
	 <head>
		 	 <title>Email Example</title>
	 </head>
	 <body>
	       This is a sample Email 
	 </body>
</html>

Step 5: Create mailable class by using the following command

 php artisan make:mail OrderInfo

Step 6: In the mailable class there is a build function. Modify the build function and add senders email and view path.

	public function build()
	{
		return $this->from('example@example.com')->view('emails.);
	}

Step 7: Call the Mail function from controller

Mail::to()->send(new OrderInfo());

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *