๐Ÿ“ง How to Send Email in Laravel โ€” SD Code Tutorial

CodeWithSdCodeCodeWithSdCode
3 min read

Sending emails is a common requirement in almost every web app. In this tutorial, I will show you step-by-step how to send a welcome email in Laravel using a simple form and a clean Blade template.

What Youโ€™ll Learn

  • Setup your .env mail configuration for Gmail

  • Create a Mailable class in Laravel 10+

  • Build a styled email form in Blade

  • Design a beautiful welcome email template

  • Send mail on form submit and show success message

Step 1: Configure Your Mail Settings

Open your .env file and add the following for Gmail SMTP:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_app_password  # Use Google App Password (see notes below)
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email@gmail.com
MAIL_FROM_NAME="SD Code"

๐Ÿ”‘ Important: If you use Gmail, create an App Password from your Google Account (you must enable 2-Step Verification). Paste that 16-character password here instead of your normal Gmail password.

Step 2: Create the WelcomeMail Mailable

Run this command in your terminal:

php artisan make:mail WelcomeMail

Now update app/Mail/WelcomeMail.php like this:

<?php

namespace App\Mail;

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

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public $email;

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

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to SD Code Tutorials'
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.welcome', // ๐Ÿ‘ˆ this is your blade file
            with: [
                'email' => $this->email,
            ]
        );
    }

    public function attachments(): array
    {
        return [];
    }
}

Step 3: Create the Welcome Email Blade View

Create a new file at resources/views/emails/welcome.blade.php and add:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f7f9fc;
            color: #333333;
            padding: 20px;
            margin: 0;
        }
        .container {
            background-color: #ffffff;
            max-width: 600px;
            margin: auto;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        h2 {
            color: #0d6efd;
            margin-bottom: 20px;
            font-weight: 700;
        }
        p {
            font-size: 16px;
            line-height: 1.6;
        }
        .footer {
            margin-top: 40px;
            font-size: 14px;
            color: #888888;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Hello {{ $email }},</h2>
        <p>Welcome to <strong>SD Code Tutorials</strong>!</p>
        <p>Thanks for joining us. We are excited to have you on board.</p>

        <div class="footer">
            <p>ยฉ 2025 SD Code. All rights reserved.</p>
        </div>
    </div>
</body>
</html>

Step 4: Create the Email Form Blade View

Create resources/views/send-mail-form.blade.php and paste this:

<!DOCTYPE html>
<html>
<head>
    <title>Send Mail | SD Code</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container mt-5">
        <div class="card shadow p-4">
            <h2 class="mb-4 text-center text-primary">๐Ÿ“ง Send Welcome Mail</h2>

            @if (session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

            <form method="POST" action="{{ route('send.mail') }}">
                @csrf
                <div class="mb-3">
                    <label for="email" class="form-label">Enter Email Address</label>
                    <input type="email" class="form-control" name="email" placeholder="example@example.com" required>
                </div>
                <button type="submit" class="btn btn-primary w-100">Send Mail</button>
            </form>
        </div>
    </div>
</body>
</html>

Step 5: Define Routes in web.php

Add this to your routes/web.php:

<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/send-mail-form', [MailController::class, 'showForm']);
Route::post('/send-mail', [MailController::class, 'sendMail'])->name('send.mail');

Step 6: Create MailController

Create a controller:

php artisan make:controller MailController

Add this code to app/Http/Controllers/MailController.php:

<?php

namespace App\Http\Controllers;

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

class MailController extends Controller
{
    public function showForm()
    {
        return view('send-mail-form');
    }

    public function sendMail(Request $request)
    {
        $request->validate([
            'email' => 'required|email'
        ]);

        Mail::to($request->email)->send(new WelcomeMail($request->email));

        return back()->with('success', 'Email sent successfully to ' . $request->email);
    }
}

Now you have a working Laravel email sending system with:

  • A sleek user form

  • A custom branded welcome email template

  • Proper routing and controller logic

This is a great foundation for adding email features to your Laravel projects.


If you want me to create more tutorials like this in SD Code style, just ask!

Happy coding! ๐Ÿš€
โ€” SD Code

0
Subscribe to my newsletter

Read articles from CodeWithSdCode directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CodeWithSdCode
CodeWithSdCode

Iโ€™m SdCode, a passionate Laravel developer sharing simple tutorials and practical coding tips to help beginners and intermediate devs grow their skills and build great projects.