How to Send Email using Mailtrap in Laravel 10


Hey there! In this tutorial, I'll guide you through the process of sending emails in Laravel 10 using Mailtrap. Mailtrap is a powerful tool that allows you to test your email functionality in a safe environment without sending real emails to your users

In this article, we'll see how to send test mail from localhost in laravel 8, laravel 9 and laravel 10.

So, let's see how to send email using mailtrap in laravel 10, laravel 10 send mail using mailtrap, laravel mailtrap send email, and send mail using mailtrap in laravel 8/9/10.

Step 1: Install Laravel 10

If you haven't already, let's start by installing Laravel 10. Open your terminal and run:

composer create-project --prefer-dist laravel/laravel my-laravel-app "10.*"

 

Step 2: Configure Mailtrap

Now, let's set up Mailtrap. Open the .env file in the root directory of your Laravel project and add your Mailtrap credentials. If you don't have an account, go to Mailtrap and sign up for free.

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-mailtrap-username
MAIL_PASSWORD=your-mailtrap-password
MAIL_ENCRYPTION=tls

Replace your-mailtrap-username and your-mailtrap-password with the credentials provided by Mailtrap.

 

Step 3: Create a Mailable

In Laravel, Mailables are used to represent email messages. Let's create a new Mailable. Run the following command in your terminal:

php artisan make:mail WelcomeEmail

This will generate a new Mailable class in the App\Mail directory.

 

Step 4: Configure the Mailable

Open the WelcomeEmail.php file in the App\Mail directory and configure the email message. Update the build method:

public function build()
{
    return $this->view('emails.welcome');
}

 

Step 5: Create the Email Template

Create the welcome.blade.php file in the resources/views/emails directory with the content you want in your email.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to Send Email using Mailtrap in Laravel 10 - Techsolutionstuff</title>
</head>
<body style="font-family: 'Arial', sans-serif;">

    <div style="background-color: #f4f4f4; padding: 20px; text-align: center;">

        <h1 style="color: #333;">Mailtrap Example in Laravel 10 - Techsolutionstuff</h1>

        <p style="color: #666;">We're excited to have you on board. Thanks for choosing us!</p>

        <div style="margin-top: 20px;">
            <a href="#" style="background-color: #3490dc; color: #fff; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Get Started</a>
        </div>

        <p style="color: #888; margin-top: 20px;">If you have any questions or need assistance, feel free to contact us.</p>

    </div>

</body>
</html>

 

Step 6: Trigger the Email

Now, let's send the email. In your controller or wherever you want to trigger the email, use the Mailable:

use App\Mail\WelcomeEmail;
use Illuminate\Support\Facades\Mail;

public function sendWelcomeEmail()
{
    Mail::to('[email protected]')->send(new WelcomeEmail());
    return "Email sent successfully!";
}

Replace '[email protected]' with the actual recipient's email address.

 

Step 7: Test in Mailtrap

Finally, run your Laravel application and trigger the email. Open your Mailtrap inbox, and you should see the test email there.

That's it! You've successfully sent an email using Mailtrap in Laravel 10.

Happy coding! 🚀

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS