DEV Community

Cover image for Laravel 9 Mail Send Using Cpanel
Arman Rahman
Arman Rahman

Posted on • Updated on

Laravel 9 Mail Send Using Cpanel

So first you have to run a artisan command for create mail file.



php artisan make:mail TestMail


Enter fullscreen mode Exit fullscreen mode

After that there will create a php file on -

app\Mail\TestMail.php



<?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 TestMail extends Mailable
{
    use Queueable, SerializesModels;
    public $data;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data; 
    }

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope()
    {
        return new Envelope(
            subject: 'Your Company Account Created Successfully!',
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content()
    {
        return new Content(
            view: 'mails.test_mail',
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array
     */
    public function attachments()
    {
        return [];
    }
}



Enter fullscreen mode Exit fullscreen mode

Here you can pass data using __constructor() function.

Now another command for create a controller for call this mail.



php artisan make:controller TestMailController


Enter fullscreen mode Exit fullscreen mode

Then it will create a controller on -

app\Http\Controllers\TestMailController.php

Add this to your email controller. Here you get $data array. You now can pass all of your data for sending through email.



//Use this on top
use Illuminate\Support\Facades\Mail;
use App\Mail\TestMail;

//Use this on Controller
$data = [
            'name' => "Arman Rahman",
        ];

        $mail = new TestMail($data);
        Mail::to($user->email)->send($mail);


Enter fullscreen mode Exit fullscreen mode

Then create blade file with your email html template.

resources\views\mails\test_mail.blade.php



<!DOCTYPE html>
<html>
<head>
    <title>Test Mail</title>
    <style>
        body {
            color: red;
        }
    </style>
</head>
<body>
    <p>Hello {{ $data['name'] }}!!</p>

</body>
</html>


Enter fullscreen mode Exit fullscreen mode

Your Basic Setup is now finished. Now set your .env file.



MAIL_MAILER=smtp
MAIL_HOST=
MAIL_PORT=
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="${APP_NAME}" 


Enter fullscreen mode Exit fullscreen mode

Create Your company email with Cpanel

Create Email From Cpanel:

Create Email From Cpanel

Click On Connect Device:

Image description

After That Get your mail confederation details from here:

Image description

Fill your .env Like this:



MAIL_MAILER=smtp
MAIL_HOST= [Incoming Server: mail.yourdomain.com]
MAIL_PORT= [Outgoing Server: SMTP Port: 465]
MAIL_USERNAME= [Username: youremail@yourdomain.com]
MAIL_PASSWORD= [Password: Your Email Password]
MAIL_ENCRYPTION= ssl
MAIL_FROM_ADDRESS= [Username: youremail@yourdomain.com]
MAIL_FROM_NAME="${APP_NAME}" 


Enter fullscreen mode Exit fullscreen mode

Now write your logic on controller and send mail.

Top comments (1)

Collapse
 
nataliiapolomkina profile image
nataliiapolomkina

Thank you for covering both the technical implementation and the necessary configuration; it is much appreciated! If you require more advanced email functionality, you can seamlessly integrate Mailtrap with Laravel. Here's a guide on the setup process and the advantages of integrating Mailtrap into a Laravel application