Step 1: Install Laravel:
You can create a new Laravel project using the laravel new command
Step 2: Create a Mailable Class
You can use the make:mail artisan command in Laravel. This will scaffold a Mailable class where your mail logic will be added.
You can specify the email subject, and the content of the email such as images, attachments, and reply to address among others.
php artisan make:mail OrderMail
This command will create a Mailable Class in the app/Mail Folder
<?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 OrderMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct()
{
//
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Order Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'view.name',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Mail Example using Markdown Template
One of the key benefits of Laravel is that it has markdown support. You can create an email template using markdown. This allows you to have a boilerplate view of the email, which you can customize to your liking or use as is.
You can modify the make:mail command and add a flag to specify which markdown will be used by your Mailable Class. The markdown template is a blade file that is placed in the “emails” directory.
php artisan make:mail OrderMail --markdown=emails.orders
This creates a default markdown blade file in the resources/views/emails folder
<x-mail::message>
# Introduction
The body of your message.
<x-mail::button :url="''">
Button Text
</x-mail::button>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
Using a simple scenario of an order mail, you can pass some default data to the markdown through the with parameter in the Mailable Class. You could use the constructor to receive data from other classes.
You can update the logic of the Mailable class
<?php
namespace App\Mail;
use App\Models\Orders;
use App\Models\User;
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;
use Illuminate\Support\Facades\URL;
class OrderMail extends Mailable
{
use Queueable, SerializesModels;
public $order, $user;
/**
* Create a new message instance.
*/
public function __construct(Orders $order ,User $user)
{
$this->order = $order;
$this->user = $user;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Your Order is being Processed',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.orders',
with:[
'order' => $this->order,
'customer' => $this->user,
'order_url' => URL::route('orders.show', $this->order->id)
]
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
The next step is to add content to the markdown blade template
<x-mail::message>
Hello {{$customer->name}},
Your Order No.{{$order->id}} has been placed successfully and is now being processed
You can view your order details using the link below.
<x-mail::button :url="$order_url">
View Order
</x-mail::button>
Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
If you want to customize the Laravel Mail Components, you can publish the assets and change them to your liking using this command
php artisan vendor:publish --tag=laravel-mail
Step 3: Sending email in Laravel
Once you have the email template set, it is now time to send an email. You can use the Mail Facade to send an email. It comes with a lot of methods but the most important is the to() method. This method defines where the email will be sent. You can pass a user’s email as a parameter and Laravel will send the email to the email specified.
The next important method is the send() method. This method allows us to specify a Mailable message Instance. This is where you specify the Mailable class you had created earlier.
use Illuminate\Support\Facades\Mail;
Mail::to(request()->user())->send(new MailableClass);
You can update the Facade to include your Mailable Class
use Illuminate\Support\Facades\Mail;
$user = User::find($id)?? auth()->user();
$order = Orders::find($id);
Mail::to($user->email)->send(new OrderMail($order, $user));
You can set the default details in the env file.
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
Once you have set the mail configurations, you can now start sending emails from your Laravel Application.
Step 4: Testing emails in Laravel
Laravel makes it easy to test emails using either the Laravel log file,mailpit, or Mailtrap as third-party integration. These services provide a fake SMTP server from where you can test and verify that your emails are correctly formatted.
All you need to do is change the mail configurations in the env file and use the credentials of any fake SMTP server.
Using Mailtrap, for instance, you can visually see how the email is formatted.