Php

How to Send Email Into Laravel 7/8/9 Using SMTP

This is a simple laravel tutorial that helps to send email using laravel 9. We will use a blade template to create a view for email content.

We will create a feedback form that has user inputs, The feedback content will send to the target email ids using the mail class.

You can send emails into laravel 9 using “mailable” class. Each type of mail sent by laravel uses the “mailable” class. All mailable classes are stored in app/Mail directory. if you don’t find in your application, don’t worry it will be generated when you will create your first mailable class using the command.

Video Tutorial:

If you are more comfortable in watching a video that explains about How to Send Email Into Laravel 7/9 Using SMTP, then you should watch this video tutorial.

Send Mail in Laravel

Laravel provides a clean, simple mail API over the great SwiftMailer library with drivers for SMTP, Mailgun, SparkPost, Amazon SES, PHP’s mail function, and sendemail.

Step 1: Install Laravel 9 application
We will start this send mail example by downloading latest laravel 0.

composer create-project laravel/laravel email-send-example --prefer-dist

Step 2: Email Configuration –
We will define the Email configuration into the .env file of laravel application.

MAIL_DRIVER=smtp
MAIL_HOST=smtp-hostname
MAIL_PORT=25
MAIL_USERNAME= // some username
MAIL_PASSWORD= // some password
MAIL_ENCRYPTION=null

You need to replace above credentials as per your company profile.

Step 3: We will create Mailable class into laravel application.Go to a terminal window and run the following command.

php artisan make:mail FeedbackMail

Above command will create a Mailable class FeedbackMail.php inside App\Mail folder. Now, this class contains one property which is a feedback content.

Related Post
<?php

namespace App\Mail;

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

class FeedbackMail extends Mailable
{
    use Queueable, SerializesModels;
    public $feedback;

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

    /**
     * Build the message.
     *
     * @return $this
     */    public function build()
    {
        return $this-&gt;view('emails.feedback');
    }
}

Above class have a parameterized constructor. We will pass feedback content as a parameters when we build this class’s instance.We have used feedback.blade.php file that are inside views/emails folder.

The view file use $feedback variable and display the message into the email template.

Step 4: Create an email view file.
We will create a feedback.blade.php file into the views/emails folder.This file have email template layout with HTML content.

<div class="well col-sm-8">{{ $feedback }}</div>

Step 5: Define the route into the laravel application.
Now, Open the routes web.php file from the routes/ folder, We will make one entry here to send feedback mail.

Route::get('/send/send_feedback', 'HomeController@sendFeedback');

Now, We need to write the code inside mail function to send an email.

// HomeController.php

use Illuminate\Support\Facades\Mail;
use App\Mail\FeedbackMail;

public function sendFeedback()
{
   $comment = 'Hi, This test feedback.';
   $toEmail = "phpflow@gmail.com";
   Mail::to($toEmail)-&gt;send(new FeedbackMail($comment));
   
   return 'Email has been sent to '. $toEmail;
}

We have imported Mailable and Mail facade for send mail.

How to test mail in Laravel 9

Now we run the application, Go to your terminal and hit the following command to run laravel application.

php artisan serve

Now open URL: http://localhost:8000/send/send_feedback into your favorite browser.

If everything is right then you will get message “Email has been sent to phpflow@gmail.com”.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories