Logo

Office Address

123/A, Miranda City Likaoli
Prikano, Dope

Phone Number

+0989 7876 9865 9

+(090) 8765 86543 85

Email Address

info@example.com

example.mail@hum.com

How to Send Email in Laravel using SMTP

How to Send Email in Laravel using SMTP

Today we are going to explain how to send email in Laravel using STMP. Laravel is PHP based framework that comes with a simple API to send emails via SMTP from localhost or Hosted online. You can easily use the 'Mailable' class to build an email function. You can also send customized data explicitly using the with() a method. Can Use SMTP Mails service providers like Google, Yahoo, Webmail and etc

Laravel Send Email via SMTP

Configure email setting in 'config\mail.php' Like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc.

Here are the steps to send mail using the SMTP server in laravel.

Step 1:
First, install the latest version of laravel. Below is the command to install Laravel

composer create-project laravel/laravel project_name --prefer-dist

Step 2:
open up the '.env' file and configure the email settings. Here we are using email client as Gmail

Gmail SMTP Settings:
Host Name: smtp.gmail.com
Smtp Port: 587(tls), 465(ssl)
Encryption: tls/ssl

Your '.env' file should look like given below 

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_gmail_id
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=ssl

Step 3:
Then generate the mailable class. Fire up the below command on the command prompt.

php artisan make:mail SendeMail

This will create 'SendeMail.php' file inside 'App\Mail' folder.

SendMail.php

namespace App\Mail;

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

class WelcomeUser extends Mailable
{
    use Queueable, SerializesModels;
 
    public $uname;

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

    public function build()
    {
        return $this->view('email.welcome');
    }
}
?>

 

Setp 4:

Create a View file. This will contain the template of our email. Create 'email' folder inside 'views' and place 'welcome.blade.php' file. below are the simple view template 

welcome.blade.php

 




   Welcome User
 


 

 


 

Hi {{ $uname }}! Welcome to KodingMadeSimple.com


 

 

 

 

Step 5:
Create a contoller file with comment give below 

php artisan make:controller MailController

And Open the 'MailController.php' file inside 'App\Http\Controllers' folder and add send_mail() function

MailController.php

...

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

...

public function send_email()
{
 $user_name = 'John Anderson';
 $to = 'john.anderson@gmail.com'
 Mail::to($to)->send(new WelcomeUser($user_name));
 return 'Mail sent successfully';
}
?>

Step 6:
Add route for our MailController send_mail() function. router file will be in this path 'routes\web.php'

Route::get('/user/sendmail', 'HomeController@send_email');

Open your browser and Run the code 

Tags

  • Sending email via Gmail SMTP server in laravel

  • Allowing users to send an email with their own SMTP settings in laravel

  • Laravel 7 send email Gmail

  • Laravel send a mail with SMTP
  • Laravel mail send an example
  • Laravel setup Gmail SMTP

  • Laravel mail configuration env

  • How to send email in laravel dynamically

  • How to Send Email in Laravel 5.6 using Gmail SMTP
  • How to Send Email in Laravel using Webmail SMTP
  • Laravel mail send an example

  • Laravel mail configuration env

Related Posts

2D Animation Development Company

2D Animation Development Company

Animation is becoming a crucial tool for branding, marketing, education, and other fields in today's digital age. Blazingcoders is a top 2D animation development company in India that specializes

Read More
H

How to convert an HTML element or document into image ?

In this article, I will be able to show you ways to convert Html div to a picture using JQuery. Here, I'm generating an HTML div element on the client-side to im

Read More
Get Ranked in Google

Get Ranked in Google

What Is SEO? Whenever you enter a query in a search engine and hit ‘enter’ you get a list of web results that contain that query term. Users normally tend to visit websites that are at

Read More