How to send email in Laravel 10

How to send email in Laravel 10

Posted on:July 16, 2023 at 04:00 PM

If you are new in Laravel or struggled with sending email in laravel 10, this post may help you. In this post, I will show you the step by step process how to send emails from your laravel 10 application by using mailtrap.

What you need?

  • A laravel application
  • An account with mailtrap (it’s free)

Table of Content

Step 1: Create a laravel application

Create a laravel application on your machine by composer.

composer create-project laravel/laravel my-app

Step 2: Set email configuration

To send email, we need email sender. It could be any mail service provider e.g. gmail, yahoo. For make it easy, I will use mailtrap. The aim of this application to able to send email from our app.

  • Create a mailtrap account: https://mailtrap.io/register/signup?ref=header

  • Then login to mailtrap: https://mailtrap.io/signin

  • Follow the following steps:

  • Now open your .env file and paste your copied value:

    MAIL_MAILER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=2525
    MAIL_USERNAME= # value from mailtrap
    MAIL_PASSWORD= # value from mailtrap
    MAIL_ENCRYPTION=tls
    MAIL_FROM_ADDRESS= # set the sender email address
    MAIL_FROM_NAME="${APP_NAME}"

Step 3: Create a route

In your app, create a route called send-mail which allows us to send mail when visit the url.

Route::get('/send-mail', [SendMailController::class, 'index']);

Step 4: Create the Controller

Since we refer the SendMailController, let’s create it.

php artisan make:controller SendMailController

Let’s update SendMailController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;
use App\Mail\SampleMail;

class MailController extends Controller
{
    public function index()
    {
        $content = [
            'subject' => 'This is the mail subject',
            'body' => 'This is the email body of how to send email from laravel 10 with mailtrap.'
        ];

        Mail::to('[email protected]')->send(new SampleMail($content));

        return "Email has been sent.";
    }
}

Step 5: Create mail class

Imagine that I want to send a sample email. Let’s create a mail class.

php artisan make:mail SampleMail

Let’s update the mail class.

namespace App\Mail;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;

class SampleMail extends Mailable
{
    use Queueable, SerializesModels;

    public array $content;

    public function __construct(array $content) {
        $this->content = $content;
    }

    public function build(): Content
    {
        return $this->subject($this->content['subject'])
            ->view('emails.sample');
    }
}

Step 6: Create mail view page

Let’s create a mail view page in resources/views/emails/sample.blade.php.

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <!--
	You can put your custom CSS if you wish
    -->
</head>
<body>
    <p>{{ $content['body'] }}</p>
    <p>Some more text</p>
</body>
</html>

Step 7: Run your code

To check your code, you need to serve your application:

php artisan serve

This will allows you to run your project in the terminal with: http://localhost:8000/send-mail

Step 8: Check your email

Now if you go to mailtrap, you should see an email there from your application.

Thanks.