Laravel : how to send email with localhost server

For sending emails from a localhost server in Laravel requires configuring your application to use an SMTP service or a “mailtrap” service for just testing. I am explaining you how to setup and send email from localhost server and make testing email things. Here’s how you can do it:

1. Set Up a Mail Service

For development purpose, Mailtrap is a great option. It captures emails in a sandbox and prevents them from being sent to real users. firstly you need to signup mailtrap website https://mailtrap.io and configured get SMTP credentials.

2. Configure/setup .env File

if you have new setup of laravel website so .env file have some default configuration setting. you need to update new credentials in .env file.
Before –

you need to replace those credentials with mailtrap credentials.

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Replace your_mailtrap_username, your_mailtrap_password, your_mailtrap_encryption and your_mailtrap_from_address with the credentials from Mailtrap.

3. Set Up a Mailable

Run the following command to generate a mailable class:

php artisan make:mail WebdeveloperMail

go to this path – app\Mail\WebdeveloperMail.php and Edit the generated WebdeveloperMail class in App\Mail\WebdeveloperMail.php:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WebdeveloperMail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Test Email')
                    ->view('emails.test');
    }
}

Create a view file under resources/views/emails/test.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Test Email</title>
</head>
<body>
    <h1>{{ $details['name'] }}</h1>
    <p>{{ $details['message'] }}</p>
</body>
</html>




4. Send the Email

you need to create new route sendemail.

Route::get('/sendemail', [App\Http\Controllers\HomeController::class, 'sendEmail'])->name('sendEmail');

in home controller you need to out below code.

use Illuminate\Support\Facades\Mail;
use App\Mail\WebdeveloperMail; // Create this Mailable class

public function sendEmail()
    {
    $data = [
        'name' => 'WEB developer India',
        'message' => 'This is a testing email from Laravel on localhost server.'
    ];
    Mail::to('[email protected]')->send(new WebdeveloperMail($data));
    return "Email sent successfully!";
    }

here I am sending you updated homecontroller.php file code.

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\WebdeveloperMail; // Create this Mailable class

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }

    public function sendEmail()
    {
    $data = [
        'name' => 'WEB developer India',
        'message' => 'This is a testing email from Laravel on localhost server.'
    ];
    Mail::to('[email protected]')->send(new WebdeveloperMail($data));
    return "Email sent successfully!";
    }
}

after put code you can check email functionality with hit this url. http://localhost/Laravel_installation/sendemail

Leave a Reply

Your email address will not be published. Required fields are marked *

82 − = 81