Integrated Tech Solutions

How to send Beautiful Emails in Laravel

By - Admin April 14, 2022

#1 Setup the SMTP configurations in ENV file

MAIL_MAILER=smtp
MAIL_HOST=mail.example.com
MAIL_PORT=465
MAIL_USERNAME=admin@example.com
MAIL_PASSWORD=SMTP_Password
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS=admin@example.com
MAIL_FROM_NAME="${APP_NAME}"

Please Note – Don’t forget to update the host and mail from address

#2 Create mail using the artisan command

Run the following command to make a mail instance in laravel

php artisan make:mail MyFancyEmail

#3 Create the Email Template

Just like views, we need to create a blade template file for email too.

Create a file named MyFancyEmail.blade.php in resources/views/email/ folder

<!DOCTYPE html>
<html>
<head>
    <title>Hello from Laravel</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['contents'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

#4 Create a Controller to send Email

Create a controller by running the following command

php artisan make:controller EmailController

This command will generate a file app/Http/Controllers/EmailController.php
add the following code in this file –

/* add this in header */
use App\Mail\MyFancyEmail;

/**************************/

class EmailController extends Controller
{
    public function enquiry(Request $request)
    {
        $mail_content = [
            'title' => 'New Email from Laravel',
            'body' => 'Silence is Golden'
         ];
Mail::to('someone@example.com')->send(new MyFancyEmail($mail_content));

    }
}

You May Also Like –

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »
next js deployed on aws

Beginners Guide to deploy NextJs App on AWS EC2 Server

In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS...

Know More »