Mail Send in Laravel 8 Design CSS3 HTML5

Mail Send in Laravel 8 Design CSS3 HTML5

 

Sending Emails in Laravel

Sending emails in Laravel is an essential feature. Laravel provides an easy-to-use email API powered by SwiftMailer, supporting multiple mail drivers like SMTP, Mailgun, Postmark, Amazon SES, and Sendmail.

This tutorial will walk you through the steps to set up email sending in Laravel with form validation.

Step 1: Install Laravel Project

First, install a Laravel project by running the following command:

composer create-project --prefer-dist laravel/laravel sendmail_laravel

Step 2: Set Up Database Credentials

Create a new MySQL database (e.g., custom_auth) and update your .env file with the database credentials.

.env

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=custom_auth DB_USERNAME=root DB_PASSWORD=

Step 3: Configure SMTP for Email

Update your .env file with your SMTP settings. If using Gmail, set the following:

MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your_email@gmail.com MAIL_PASSWORD=your_email_password MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=your_email@gmail.com MAIL_FROM_NAME="${APP_NAME}"

⚠️ Important: If using Gmail, enable Less Secure Apps or create an App Password.

Step 4: Create Email Routes

Define routes for showing the form and sending emails.

routes/web.php

use App\Http\Controllers\EmailController; Route::get('/email', [EmailController::class, 'create']); Route::post('/email', [EmailController::class, 'sendEmail'])->name('send.email');

Step 5: Create the Email Controller

Create a new controller for handling emails:

php artisan make:controller EmailController

app/Http/Controllers/EmailController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Mail; class EmailController extends Controller { public function create() { return view('email'); } public function sendEmail(Request $request) { $request->validate([ 'email' => 'required|email', 'subject' => 'required', 'name' => 'required', 'content' => 'required', ]); $data = [ 'subject' => $request->subject, 'name' => $request->name, 'email' => $request->email, 'content' => $request->content, ]; Mail::send('email-template', $data, function($message) use ($data) { $message->to($data['email']) ->subject($data['subject']); }); return back()->with('message', 'Email successfully sent!'); } }

Step 6: Create the Email Form View

Create a Blade view to display the email form.

resources/views/email.blade.php

<!DOCTYPE html> <html lang="en"> <head> <title>Send Email - Laravel</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h2>Send an Email</h2> @if(session()->has('message')) <div style="color: green;"> {{ session('message') }} </div> @endif <form action="{{ route('send.email') }}" method="post"> @csrf <label>Name</label> <input type="text" name="name" required> @error('name') <span style="color:red">{{ $message }}</span> @enderror <label>Email</label> <input type="email" name="email" required> @error('email') <span style="color:red">{{ $message }}</span> @enderror <label>Subject</label> <input type="text" name="subject" required> @error('subject') <span style="color:red">{{ $message }}</span> @enderror <label>Message</label> <textarea name="content" required></textarea> @error('content') <span style="color:red">{{ $message }}</span> @enderror <button type="submit">Send Email</button> </form> </body> </html>

Step 7: Create an Email Template

Create a Blade template for the email content.

resources/views/email-template.blade.php

<!DOCTYPE html> <html lang="en"> <head> <title>Email Template</title> </head> <body> <h2>Hello, {{ $name }}!</h2> <p>{{ $content }}</p> </body> </html>

Step 8: Test the Email Sending

Run your Laravel application:

php artisan serve

Visit:

http://127.0.0.1:8000/email

Fill in the form and submit it. If everything is correct, the email will be sent.

Bonus: Using Laravel Mailables (Recommended)

Instead of using Mail::send(), you can use Laravel Mailables for a cleaner approach.

Create a Mailable Class

php artisan make:mail SendMail

Update app/Mail/SendMail.php

<?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; public $data; public function __construct($data) { $this->data = $data; } public function build() { return $this->subject($this->data['subject']) ->view('email-template'); } }

Modify the Controller

use App\Mail\SendMail; use Illuminate\Support\Facades\Mail; public function sendEmail(Request $request) { $request->validate([ 'email' => 'required|email', 'subject' => 'required', 'name' => 'required', 'content' => 'required', ]); $data = [ 'subject' => $request->subject, 'name' => $request->name, 'email' => $request->email, 'content' => $request->content, ]; Mail::to($data['email'])->send(new SendMail($data)); return back()->with('message', 'Email successfully sent!'); }

Congratulations!

You have successfully implemented email sending in Laravel. 🚀

Features Covered:

✔ Email Form
✔ SMTP Configuration
✔ Email Sending via Mail Class
✔ Email Blade Template
✔ Form Validation
✔ Using Laravel Mailables (Bonus)

Need any improvements like attachmentsqueueing, or dynamic templates? Let me know! 

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

3 Comments

CAN FEEDBACK
  1. Unknown
    Unknown
    i am not able to send the name from the contact form. can you tell what can I do?
  2. https:Garenafreefire.blogger
    https:Garenafreefire.blogger
    (y)
  3. Anonymous
    Anonymous
    hi, the email is not sent!! can u help plz
close