Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram
Readytocode
  • Home
  • Laravel
  • PHP
  • React Js
  • Git
  • Server
Facebook X (Twitter) Instagram Pinterest LinkedIn Reddit
Readytocode
You are at:Home Laravel 9 send mail using SMTP

Laravel

Laravel 9 send mail using SMTP

JustinBy JustinSeptember 5, 2022No Comments3 Mins Read
Facebook Twitter Pinterest LinkedIn Tumblr Email




Hello , in this post i will show you how we can send email using SMTP, here i will use gmail SMTP and show you, also i will use a beautiful email templates that is created with HTML.

Let Start:

Step 1. Install Laravel

I hope you already aware with Laravel and its installations, If not please use this link.

 

Step 2. Changes in .env file

put the below code and change with your email and password.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=gmailpassword
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Step 3. Create Mailable Class

Now in this step we will create mailable class by using below command .

php artisan make:mail AnyClassName

Once above code will run you can see a new directory inside app->Mail created. inside Mail your new class is also created.

check the below file. Here my class name is SendMail.

<?php

namespace App\Mail;

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

class SendMail 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');
    }
}

Now at bottom , there is function name build, let’s change with your email view, like below code.

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

 

Step 4. Create a blade file for email

In this step we will create a html template which will view on your email inbox. I will create a directory named resources/views/email, and inside i will create testmail.blade.php.

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 9 send mail using SMTP</title>
</head>
<body>
    <h1>This is test mail from readytocode.net</h1>
    <h2>Welcome developer , You are great. </h2>
</body>
</html>

Step 5. Create a controller and define route

Now in this step we will create a controller and define it’s route

For creating a controller we will use the below command.

php artisan make:controller SendEmailController

A new file is created, Just create a new method like below.

public function index()
    {
      Mail::to('[email protected]')->send(new SendMail());
 
      if (Mail::failures()) {
           return response()->Fail('Sorry, Unable to send!');
      }else{
           return response()->success('Success! Check your inbox.');
         }
    }

Now create route for the method

Route::get('sendemail', [SendEmailController::class, 'index']);

Now all set let run your project and hit sendmail

php artisan serv
http://127.0.0.1:8000/sendemail

If it will run then okay, if you are using Gmail then a error will show like below.

Swift_TransportException
Failed to authenticate on SMTP server with username "[email protected]" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "534", with message "534-5.7.9 Application-specific password required. Learn more at 534 5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor n9-20020a170902e54900b001714853e503sm7757148plf.36 - gsmtp ". Authenticator PLAIN returned Expected response code 235 but got code "534", with message "534-5.7.9 Application-specific password required. Learn more at 534 5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor n9-20020a170902e54900b001714853e503sm7757148plf.36 - gsmtp ". Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials n9-20020a170902e54900b001714853e503sm7757148plf.36 - gsmtp ".

 

So for solving this, let’s login to your gmail. And click on manage account like below image.

Then navigate to App like below image

app create

 

Choose custom

App create

A password is given on pop up.

App Password

 

Now change to .env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=NEW_APP_PASSWORD
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

 

Now all set, Enjoy email .

Thanks for reading, Happy Coding..

 

Recommended Laravel Post

Read how to use datatable in laravel9

How to use ckeditor in laravel9

 

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Share 0

Like this:

Like Loading...

Related

Laravel 9 laravel email laravel smtp laravel9 email
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Justin
  • Website

Related Posts

How to Generate image from text with Laravel 10

May 16, 2023

Laravel 10 Restrict User Access

May 3, 2023

Laravel 10 Pagination Example

May 3, 2023

Leave A Reply Cancel Reply

Featured Posts
  • Why meta tag is important for seo 1
    How to change react Page title, Page Description and some other meta Tag
    • August 4, 2023
  • 2
    How to Generate image from text with Laravel 10
    • May 16, 2023
  • Laravel 10 Restrict User Access 3
    Laravel 10 Restrict User Access
    • May 3, 2023
  • Laravel 10 Pagination Example 4
    Laravel 10 Pagination Example
    • May 3, 2023
  • install Steam on Ubuntu 5
    How to install Steam on Ubuntu 22.04 step by step
    • April 20, 2023



Readytocode
Facebook X (Twitter) Instagram Pinterest
  • About Us
  • Privacy Policy
© 2025 ReadyToCode.

Type above and press Enter to search. Press Esc to cancel.

%d