Laravel Sending Emails | The Easiest and Simple Way

TheCodeLearners
May 15, 2020 · 5 min read
Laravel Sending Emails | The Easiest and Simple Way
Laravel Sending Emails | The Easiest and Simple Way

Sending emails are generic tasks done my more applications. And core PHP also provides you with a mail() function for sending the email. But developers at laravel made is an interesting and intuitive task when it comes to sending emails.

Laravel Developer has done a very appreciable job and also provided with different drivers and the option of sending emails. Some of those drivers are mailgun, postmark, Amazon SES driver, SMTP etc.

Note

In this post, we’ll be covering topic regarding sending emails through laravel using SMTP services.

Configuring Mails using SMTP settings

In your project root, you’ll find .env the file which consists of all the environment setting for laravel application. There replace MAIL the section with below code.

The above is a simple and easiest configuration for sending the email. Our email drive in this case is MAIL_DRIVER and I’m using MAIL_HOST of google which provides free SMTP settings for sending an email.
Also, goto this google app password setting page to create an app password for your email as the normal password doesn’t work.

Generating a Mailable class

The best point about laravel mailable is that it keeps code and email template separately and this makes managing multiple email class a simple task.
The below command will generate a mailable class in your project directory laravel_email/app/Mail/WelcomeEmail.php.

The newly generated mailable class looks like below.

The __construct() method is a magic method through which parameters can be passed to WelcomeEmail mailable class.
build() method prepares or binds class data to views for sending the email. But does not send unless it is passed through send() method of Mail class.
This method is automatically triggered by the class itself.

In the build() method you have to specify view for this email, from sender information and also must be able to pass variables to views. After modification, our WelcomeEmail class looks like below.

Variables passed through constructor are assigned to $this->email_data which are in turn passed to email view through the with() method.

Creating a view for mailable class

Views will be sent are email. It is just a design part where necessary data is displayed.
For creating a view file goto laravel_email/resources/views/emails/welcome-email-template.blade.php.

Create Controller

Define Route

Preview email before sending

previewEmail method take $data as a parameter which consists of information which will be used in views file.

If just want to see how end-user sees then just return new WelcomeEmail($data) object and respective view for this particular email class will be displayed into the browser.

Sending email synchronously

Create a route for sending email synchronously.

For sending emails in synchronous manner use send() method which is provided by Mail facades. You must use mail facade before sending an email.
In laravel_email/app/Http/Controllers/HomeController.php

Below will also work and will send an email directly specified as a parameter in to() method.

Sending email as queued job

Create a route for sending email as a queued job.

For sending mails through background process use the queue() method provided by facade Mail insisted of send() method.
What this does is that insisted of sending an email immediately it queues it into a particular connection which must be manually configured.

Using queue() method is faster and robust than using send() during production.

For queueing emails additional configuration must be done manually for more information on the queue you can read our Laravel Jobs and Queues — Configuring, Sending Mail, Dispacting Jobs post.
For official documentation, you can visit Laravel Mails.

Conclusion

You have come to end of Laravel Sending Emails | The easiest and simple way post. Support us by sharing this post which will help us grow and comment if you have any doubts we will reach you soon.

This post is originally published on thecodelearners site

The Code Learners

Helpline for Software Development

The Code Learners

We provide software development services and post articles on different programming language explaining various aspects thoroughly.

TheCodeLearners

Written by

I’m are technology blogger and publish post related to software development. You can visit my website by clicling on this link http://thecodelearners.com/

The Code Learners

We provide software development services and post articles on different programming language explaining various aspects thoroughly.