laravel send HTML mail

 STEP 1: make a controller as 'Email'

<?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Mail;

use App\Mail\testMail;


class Email extends Controller

{

    public function index()

    {

        Mail::to('meshimanta@yahoo.com')->send(new testMail());

        return true;

    }

}



STEP 2: make a mail component by "php artisan make:mail testMail"

<?php


namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Mail\Mailable;

use Illuminate\Mail\Mailables\Envelope;

use Illuminate\Queue\SerializesModels;


class testMail extends Mailable

{

    use Queueable, SerializesModels;


    public function __construct()

    {

        //

    }


    public function build()

    {

        $name = 'SHIMANTA DAS';

        $content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

    

        return $this->from(env('SEND_MAIL_ACCOUNT'), 'microcodes.in')

            ->view('testmail',['name'=>$name, 'content'=>$content]);

    }


    /**

     * set the email 

     */

    public function envelope(): Envelope

    {

        return new Envelope(

            subject: 'Test Mail',

        );

    }


    public function attachments(): array

    {

        return [];

    }

}


STEP 3: make a blade file as 'testmail.blade.php'

<!doctype html>

<html lang="en">

  <head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Bootstrap demo</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

  </head>

  <body>

  <br>

  Hi! i'm {{$name}} sending you this email.

  <br>

  <p>{{$content}}

  </p>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>

  </body>

</html>


STEP 4: make a route too.

<?php


use Illuminate\Support\Facades\Route;

use App\Http\Controllers\Email;

Route::get('/send',[Email::class,'index']);



STEP 5: make a .env and enter right credentials

MAIL_MAILER=smtp

MAIL_HOST=live.smtp.mailtrap.io

MAIL_PORT=2525

MAIL_USERNAME=api

MAIL_PASSWORD=64fa2638ea1098dd3ee732a

MAIL_ENCRYPTION=tls

SEND_MAIL_ACCOUNT=mailtrap@demomailtrap.com





Popular posts from this blog

MCSL 216 MCA NEW Practical ~ common questions suggestions

dev chaeatsheet

STRAPI