Tuesday 2 June 2020

Sending an Email with Users Own SMTP Settings in Laravel

Sending an Email with Users Own SMTP Settings in Laravel

One of the most common features that every web app offers is sending emails. Some sites make use of their email feature to send newsletters, some utilize it for gathering feedbacks. Websites that are PHP-powered make the Mail() method for email sending.

Nonetheless, the security in this case is often jeopardized since this is unfortunately not the most secure way of sending and receiving emails. To send emails in the Laravel framework, it provides numerous advanced features.

With various available packages in the market, integration of the emailing functionality becomes effortless. Laravel developers could help eCommerce brands in simplifying the email sending process through creating effective solutions.

Why Laravel for sending Emails?

Laravel provides different tools for configuring website emails. Some of the options are the following:
  • Laravel offers email queueing options
  • It proposes the use of drivers for Mailgun, SMTP, Amazon SES, SparkPost, as well as sendmail
  • File attachment of various formats, like PDF, JPEG, and other with the framework’s Templating system, enabling you to use different templates and configuring the view.
  • Markdown support could be used to build amazing templates, such as panels or buttons and many more.

The Robust Laravel Framework

Laravel is a sturdy and robust framework of MVC PHP aimed to empower developers looking for an elegant and seamless toolkit, which helps develop web apps that are full-featured. Laravel developers deliver business solutions that are exquisitely simple, easing the coding of app further.

Considered as one the most renowned frameworks of PHP for many reasons, the framework for eCommerce business provides an entire list of features, as well as other measures for convenience, a critical factor in the field of web application development.

Moreover, the Laravel framework also offers codes separation, a dedicated dependency manager, routing services, unit testing, modular packaging and numerous ways of gaining relational databases, as well as all the provisions that drive the business popularity of the framework further.

Sending Emails of Users with their own SMTP Laravel Settings

There’s a method for users to send email with their own SMTP credential. The goal is to provide a more personal look and touch to all outgoing emails and make it appear as if it directly came from a company, against a generic one for web application.

eCommerce businesses engaging the services of a Laravel development company would want to make emailing features simple and easy for their clientele. Sending quick emails in a project is possible by setting up the environment variables with valid SMTP credentials.

The Tutorial

Let me discuss in this tutorial the way to configure the applications of Laravel for sending emails using your own Gmail account, since the Gmail SMTP server with the default SMTP configurations of the framework. It could not be over emphasized, the benefits of the SMTP server. You could send email from a local server with the SMTP server.

On its own local server, it provides an ability of testing the functionality of email. Mails sent via the SMTP would not be flagged by Gmail as SPAM, as seen in mails sent with the PHP platform’s default mail function.

Let’s follow these simple steps to get started:

Step One: Configuring the Gmail SMTP Server in a Laravel App

Laravel makes use of config/mail.php file to store the used details in sending mail. The file has settings, such as MAIL_HOST, MAIL_DRIVER and MAIL_PORT and others. To successfully send an email, these information should be provided.

To add the information required, we have to edit the config/mail.php file instead of supplying details in the env.file correspondingly. So, open the env.file located in your app’s root directory and checkout the following settings:


Edit the above details as follows.


In the settings above, the MAIL_DRIVER as smtp we have configured, MAIL_HOST for Gmail as smtp.googlemail.com, MAIL_ENCRYPTION method as ssl and MAIL_PORT for the Gmail as 465.

You must replace the MAIL_USERENAME and the PASSWORD with the Gmail email add and the password respectively. Because we’re using the Gmail SMTP, we have to change several security settings on our Google account, to provide access to apps that are less secured.

Step Two: Configuring the Google Account

Login to the Google email account and then click on the Google Account button. This button is displayed when clicking on your profile pic in the Gmail Dashboard, as shown below.

Sending an Email with Users Own SMTP Settings in Laravel

When you reach the My Account Page, click on the Security, then scroll to the bottom and you would see the ‘Less secure application access’ settings. Click on the radio button to set it to ON mode.

Step Three: Email sending from your Laravel App

All the basic setup process at this point has been accomplished. Now, we can proceed to write a few Laravel PHP codes for sending an email. To begin, create a controller of your choice in which the sending mail logic would be handled. Then, write your codes in this controller with the below snippet as your guide:

$to_name = ‘RECEIVER_NAME’;
$data = array(‘name’=>”Ogbonna Vitalis(sender_name)”, “body” => “A test mail”);
Mail::send(‘emails.mail’, $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject(Laravel Test Mail’);
$message->from(‘SENDER_EMAIL_ADDRESS’,’Test Mail’);

We utilize our mail template in the code above as file 'email.mail' so we must create 'emails' folder and a mail.blade.php file at resources\views\emails\mail.blade.php

There should be various test codes to the test mail template mail.blade.php, the same as what’s you see below.

Hello <strong>{{ $name }}</strong>,

And with that, we are done. Creating any route of choice and star sending emails from your Laravel application could be done seamlessly.

The Top Laravel Emailing Tools

There are numerous email sources and packages available for the Laravel framework. Some of the most commonly used tools for sending emails are the following:
  1. Mailgun
  2. Mailtrap
  3. Swiftmailer
  4. Mandrill
  5. Sendgrid
  6. Mailchimp


We the setup we created, we could send emails with configured custom credentials during runtime. What’s great is that we don’t see the need to reach out to any packages that solve the particular concern. We further utilized similar orchestration and objects used by the framework under the hood for email sending.

With the use of pre-built tools, sending emails in a Laravel app is easy and fast. Moreover, as we have mentioned above, the process of integration is quite straightforward, thus developers could get started with the Laravel emailing tools swiftly.

Author Bio:

Olivia Diaz is working at eTatvaSoft, an Enterprise level Web & Mobile Application Development Company. Being a tech geek, she keeps a close watch over the industry focusing on the latest technology news and gadgets. Follow me on Twitter.


Post a Comment