🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to send 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


Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.