Pages

Tuesday, April 19, 2016

Send emails using Gmail in Laravel 5.1

Laravel provide a simple API over the popular SwiftMailer library. You may use a plain PHP method to send emails, or you may use some email service providers such as Mailgun, Sendgrid, Mandrill, Amazin SES, etc.

To send emails, simply edit the mail.php configuration file, which is placed in the config directory.


The mail.php file should look like this,


 After editing the mail.php file, edit the .env file which should look like this,



(MAIL_USERNAME and MAIL_PASSWORD should be replaced with sender’s E-mail address and password)

Lets now send a test email

To send a test email open routes.php file and add this route



As you see, we use the send method on the Mail façade. There are three arguments,
  • The name of the view that we use to send emails.
  • An array of data that we want to pass to the email.
  • A closure that we can use to customize our email subjects, sender, recipients, etc.

Finally we have to create welcome.blade.php which would look like this


Because we’ve passed an array containing the $name key in the above route, we could display the name within the welcome view using {{ $name }}

Check your inbox, you should receive a new email.

In case if you get any errors saying ‘no permission provided to access your mail account’, simply login to your relevant Gmail account and then click the below link,


Take a look at the Sign-in & Security -> Connected apps & sites -> Allow less secure apps settings.

You must turn the option “Allow less secure apps” ON.

Now try sending email again, it will work fine.

I hope that you got an idea on how to send email using Gmail in Laravel 5.1

No comments:

Post a Comment