laravel example
About Us Blog Contact Us Request a quote

How to send email in laravel 12

Install Laravel 12 App

Run below this command


composer create-project laravel/laravel example-app

Setup mysql database

update .env file


MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-mailtrap-username
MAIL_PASSWORD=your-mailtrap-password

create route

routes/web.php like as below code


<?php

use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});


Route::controller(MailController::class)->group(function () {
    Route::get('/send-email', 'sendEmail')->name('sendEmail');
  
});

create mail file

Run below this command


php artisan make:mail SendEmail

app\Http\Mail\SendEMail.php like as below code


<?php

namespace App\Mail;

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

class SendEmail extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Laravel 12 email test',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'email.email-template',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}

Create controller


php artsan make:controller MailController

app\Http\Controllers\mailController.php like as below code


<?php

namespace App\Http\Controllers;

use App\Mail\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function sendEmail(Request $request)
    {
        $variable="Hello artisan!";
        $recipient='hello.laravelexample@gmail.com';
        Mail::to($recipient)->send(new SendEmail($variable));       
        return response()->json(['success'=>'Send email successfully.']);
    }
}


Create email template blade file

resource\email\email-template.blade.php like as below code


<!DOCTYPE html>
<html>
<body>
<title>https://www.laravelexample.com</title>

<h2>{{$variable}}</h2>

<p>This is example of laravel 12 email.</p>

</body>
</html>

Run Laravel App:

Now Run your laravel 12 app after run these below commands


php artisan serve

Go to your web browser, hit this URL http://localhost:8000/send-email and see your laravel app output:




how to send email in laravel 12

Recent Posts