Skip to content

Instantly share code, notes, and snippets.

@deepak-cotocus
Last active May 6, 2020 15:58
Show Gist options
  • Save deepak-cotocus/5b7219a9fc2a6f86a289e4f7a5584264 to your computer and use it in GitHub Desktop.
Save deepak-cotocus/5b7219a9fc2a6f86a289e4f7a5584264 to your computer and use it in GitHub Desktop.
Sending Email in Laravel

Assumption

\app\Http\Controllers\TestController.php This is the file which will invoke email sending logic.

Steps

1. Open Git bash terminal and run the below command:

php artisan make:mail TestEmailSender
  • The above command will create \app\Mail\TestEmailSender.php with below code
     <?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TestEmailSender 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');
    }
}
  • Let's Modify TestEmailSender.php with below code
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;

class TestEmailSender extends Mailable
{
    use Queueable, SerializesModels;

    private $emailParams;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($params)
    {
        $this->emailParams = $params;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->from(Config::get('app.senderEmail'),Config::get('app.senderName'))
        ->subject($this->emailParams->subject)
        ->view('mail.TestEmail')
        ->with(['emailParams' => $this->emailParams]);
    }
} 

2. Create \app\Controllers\TestController.php with below code

<?php

namespace App\Http\Controllers;

use App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
use App\Mail\TestEmailSender;
use Illuminate\Support\Facades\Log;

class TestController extends Controller
{
    public $name;
    public $email;

    public function __construct(){}

    public function sendEmail()
    {               
            $this->name = "Deepak"; //recipient name
            $this->email = "deepak.cotocus@gmail.com"; //recipient email id
            /**
             *creating an empty object of of stdClass
             *
             */
            $emailParams = new \stdClass(); 
            $emailParams->usersName = $this->name;
            $emailParams->usersEmail = $this->email;
           
            $emailParams->subject = "Testing Email sending feature";
            Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams));
    }   

    public function test(){            
           $this->sendEmail();
    }
}
  • In The above code Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams)); is invocking email sending logic and passing user's name and user's email along with Subject of email which is assigned to $emailParams object.

NOTE

Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams));

This line will invoke build() function of TestEmailSender and an email with subject and message will be sent to usersEmail successfully.


3. Create \resources\views\mail\TestEmail.blade.php with below code

     Welcome, {{ $emailParams->usersName }}
     Sending mail for Testing Purpose.
     
     Thank You

4. Add below code in web.php

Route::get('/sendemail', 'TestController@test')->name('sendemail');

5. Configure Gmail SMTP Server in Laravel Application

  • open your .env file and checkout for this settings:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<your-Gmail-Id>
MAIL_PASSWORD=yorgeneretedpassword
MAIL_ENCRYPTION=tls
MAIL_FROM=<Sender's-Gmail-Id>
MAIL_FROM_NAME=<Sender's-Name>
  • open /config/app.php and add below 2 lines in retun statement
return [
'senderEmail' => env('MAIL_FROM', '<Sender's-Gmail-Id>),
'senderName' => env('MAIL_FROM_NAME', '<Sender's-Name>'),
];

NOTE- Check Out for Gmail Email server set up


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment