Email Send Tutorial in Laravel 11

 <?php


namespace App\Http\Controllers;

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

class EmailController extends Controller
{
    public function sendEmail()
    {
        $toEmail = "findjquery@gmail.com";
        $moreUser = "heernayakpara997@gmail.com";
        $message = "Hello, Welcome to our website";
        $subject = "Welcome to YahooBaba";
        $details = [
            "name" => "John Doe",
            "product" => "Test Product",
            "price" => 250
        ];

        // send same email to multiple users as shown below :
        // $emails = [
        //     "user1@gmail.com",
        //     "user2@gmail.com",
        //     "user3@gmail.com",
        // ];

        // foreach($emails as $recipient) {
        //     Mail::to($recipient)->send(new welcomeemail($message, $subject));
        // }

        // Below cc means "carbon copy" for send mail to another user
        $request = Mail::to($toEmail)->cc($moreUser)->send(new welcomeemail($message, $subject, $details));
        dd($request);
    }
}
Above File is app\Http\Controllers\EmailController File




Below File is app\Mail\welcomeemail.php File
<?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 welcomeemail extends Mailable
{
    use Queueable, SerializesModels;
    public $mailmessage;
    public $subject;
    private $details;
    // If we take above variable private or protected then we have to declare that variable in "content()" method
    // as shown below :

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

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: $this->subject,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'mail.welcome_mail',
            // text: "mail.welcome_mail",
            // Here above, we have declared "details" variable as private(or protected)
            // Hence, we have to declare below "with" method as shown below :
            // with: [
            //     "name" => $this->details["name"],
            //     "product" => $this->details["product"],
            //     "price" => $this->details["price"]
            // ]
        );
    }

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





Below File is resources\views\mail\welcome_mail.blade.php File
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{ $subject }}</title>
</head>
<body>
    <h3>{{ $subject }}</h3>
    <p>{{ $mailmessage }}</p>
    {{-- <p>{{ $details["name"] }}</p>
    <p>{{ $details["product"] }}</p>
    <p>{{ $details["price"] }}</p> --}}

    {{-- If we have declared variable as private or protected in Mailable class then we have to declared that
    variable in blade file as shown below : --}}
    <p>{{ $name }}</p>
    <p>{{ $product }}</p>
    <p>{{ $price }}</p>
</body>
</html>




Below File is routes\web.php File
<?php

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

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

Route::get("send-email", [EmailController::class, "sendEmail"]);





Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11