Blog

Sending Emails With Laravel 5 & Gmail

Posted on
Sending Emails With Laravel 5 & Gmail

Laravel provides different options for sending emails. Laravel supports both SMTP and PHP’s “mail” function as drivers for the sending of e-mail. Some of the drivers are SMTP, Mail, SendMail, and services suported include   Mailgun, Sendgrid, Mandrill, Amazon SES, etc.

Mail Configuration

Before we can send emails with Laravel 5 we need to configure the config/mail.php file

If you have a gmail account, it’s very easy to send emails using Laravel 5!

First, go to your account dashboard and click on Connected apps & sites

Alternatively, click on this link

https://myaccount.google.com/security#connectedapps

Take a look at the Sign-in & security -> Connected apps & sites -> Allow less secure apps settings. You must turn the option “Allow less secure

Once complete, edit the .env file:

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

That is it. All emails sent through your application will be sent through you gmail account.

Note for some reasons that Google may block this action and send you a mail to verify allowing your server to sign into Gmail.

Blog

Laravel Mailer Package For Organizing Your Emails

Posted on
Laravel Mailer Package For Organizing Your Emails

When working on large applications, you may want to organize and separate each mail functions into their classes instead of writing long mail functions inside your controller or on just one long file.

We have created a package that can help you achieve that. https://github.com/stanwarri/mail-dispatcher

Installation

Add this to your composer.json

"stanwarri/mail-dispatcher": "dev-master"

  1. Create a Mail Handler

You can then create a folder where all your email sending classes are stored. For use we created a folder called “Mailers” inside the “app” folder

The For each mail function, you can then create it’s class like this. For example, Sending confirmation email

namespace App\Mailers

use Illuminate\Contracts\Mail\Mailer;

class SendConfirmation
{
protected $user;

public function __construct(User $user){
$this->user = $user;
}

public function mail(Mailer $mailer){
$this->user->
$confirmation_code = str_random(32);
$this->user->save();

$mailer->send(
'emails.confirmation',
[
'user' => $this->user,
'code' => $this->user->confirmation_code
],
function ($message) {
$message->to($this->user->email)->subject(sprintf('%s %s', config('app.title'), 'Email Verification'));
});
}}

Note: You can type hint mail() with Objects you want resolved out of the Container (As is Mailer in the example).

  1. Just add the SendsMail trait to any class you want to dispatch a mail like so:
use Stanwarri\MailDispatcher\SendsMail;

  use App\Mailers\SendConfirmation

  class UserAuthController
  {    
    use SendsMail;    

    public function register(){        
        $user = User::whereId(1)->first();        
        $this->mail( new SendConfirmation($user));    
    }
  }  
Blog

Creating dynamic select dropdown list with laravel 5.1

Posted on
Creating dynamic select dropdown list with laravel 5.1

Laravel makes creating a dynamic dropdown list incredibly simple. Let’s say for example, you have a Category model, which holds the categories you want to use in a dropdown list. You can just use the lists method pull out the values you want for the dropdown list:

PostController.php

$categories = ['' =--> 'Select a Category'] + Category::lists('name', 'id')->all();

This assumes you have id and name columns in your categories table. Then you just need to pass $categories to your view:

return view('post.create', compact('categories'));

That returns an array with the values you need, which you can just pop into your dropdown list in your view. I’m using the Collective Form Helper package for my select:

{!! Form::select('category_id', $categories) !!}

And that’s it, you get your dropdown list. Little helpers like this make Laravel a pleasure to work with.

I hope you have enjoyed this tutorial and found it useful. Please comment, share, and like if you can, thanks!