Sending emails is a crucial event in any web application. Using the Laravel framework, one of the most popular tools in web-development based in PHP, allows us to send an email through many drivers such as SMTP, Mailgun, SparkPost, Amazon SES, PHP's mail
function and sendmail
as of the current framework's release of Laravel 5.7.
We are going to explore the nitty-gritty on how Gmail uses the SMTP driver of Laravel's SwiftMailer
API to send emails easily in our applications. So, why even send an email in the first place? Use-cases may include:
- Account/Email verification upon registering in a site.
- Sending a password reset link to a user who forgot their password.
- An email option is also used when sending notifications to users.
- Managing subscriptions.
- Sending invoices on e-commerce web apps...and many more.
Now let's get into the good stuff
Create a Mailable
Assuming you have a Laravel app already installed on your particular environment, head over your command-line and type
php artisan make:mail GmailExample
This artisan command enables the creation of a Mailable
class which is the base of an email. Let's name it GmailExample. If we open the Mail directory in app/Mail, there we can see our newly created mailable
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class GmailExample 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');
}
}
For this example, we are going to send a very simple email that carries its variables explicitly and uses a markdown instead of plain text or basic html.
Markdowns are brilliant for quick styling which reduces the pain off of a developer. So modify the build function so that it has a sender, subject, receives a markdown and a few variables.
public function build()
{
return $this->from('mail@example.com', 'Bryce Andy')
->subject('Our Example')
->markdown('mails.exmpl')
->with([
'name' => 'Joe Doe',
'link' => 'http://www.bryceandy.com'
]);
}
Customize Your Markdown
Now we can create our markdown. Inside the directory resources/views, create a file called mails
and inside it a blade template name it example.blade.php
.
This is because we specified the location of the mailable as mails.example. Markdowns use components to determine the structure of its content. Modify your example blade template like the following
@component('mail::message')
Hi <b>{{$name}}</b>,
Congrats for delivering this email!
It looks neat, no?<br />
@component('mail::button', ['url' => $link])
Take Me Back <br/>
@endcomponent
Regards,<br />
DevBlog.
@endcomponent
Set Up a Route
In order to send this email we need to specify a route like the following in the routes/web.php file
use App\Mail\GmailExample;
use Illuminate\Support\Facades\Mail;
Route::get('send-mail', function () {
Mail::to('write your email here')->send(new GmailExample());
return view('/');
});
But now if we try to visit send-mail
we are going to encounter an error
Allow 2-factor Authentication & Create an App Password
This is the most important step in order to make this Gmail service work perfectly for your emailing. You might perfect every step but if you miss any step of this section then you must be running into a couple of SwiftMailer
exceptions and other errors on authentication with Gmail.
On your Gmail account go to My Account > Sign In & Security > Sign In and you can see a section where you click to enable 2-Factor Authentication. After that is done, click on the following section App Passwords and create an app password.
Mail Configuration
After creating your app password, back to your Laravel project and inside .env
variables, make sure your configs match this
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourGmailEmail
MAIL_PASSWORD=yourAppPasswordGeneratedFromGmail
MAIL_ENCRYPTION=tls
Before sending the email we should make sure we change the app name in config/app.php because the footer of the email will display the app name. Now we can hit the send-mail
route to send our email, and voila
Join to participate in the discussion