Send Emails in Laravel using Mailtrap
Laravel is a web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Laravel uses the SwiftMailer library to send emails. Using the library function, we can easily send emails without too many hassles.
In this article, we use Mailtrap for Sending an Email.
Mailtrap is a “fake SMTP server” used for development purposes. Instead of having to test your code with your own email account, & potentially flooding your inbox with test emails, you can instead use Mailtrap as the endpoint. Mailtrap is Laravel’s default SMTP server, so it’s very easy to integrate with.
Why Use Laravel to Send Emails?
Laravel offers various tools to configure email on websites. You can check out some options below:
- Laravel proposes using drivers for SMTP, Mailgun, SparkPost, Amazon SES, and sendmail
- Laravel provides options for queueing emails.
- In Laravel, you can use Markdown support which is available in a few frameworks. It helps to create awesome templates like buttons or panels and much more.
- You can attach files of different formats (JPEG, PDF), etc. using the Laravel Templating system, which lets you use various templates and configure the view.
Step 01: Create Laravel Project
Open Terminal & run the following command to create a Laravel app:
composer create-project --prefer-dist laravel/laravel laramail
Step 02: Setup .env File
Goto: https://mailtrap.io/inboxes and in the action tab click the settings icon.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<USERNAME>
MAIL_PASSWORD=<PASSWORD>
MAIL_ENCRYPTION=tls
Step 03: Create Mailable
In this step, we will create a mailable class SendTestMail for sending an email. For creating a Mailable class run the below command:
php artisan make:mail SendTestMail
app/Mail/SendTestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendTestMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Send Test Mail From Laravel')
->view('mails.testMail');
}
}
Step 04: Create View File
In this step, we will create a view file and write an email that we want to send to the user. now we just write some dummy text. create blade files In the mails folder.
resources/views/mails/testMail.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>How to Send an Email in Laravel - NPC</title>
</head>
<body>
<h1><a target="_blank" href="https://example.com">example.com</a></h1>
<h3>User Detail:</h3>
<h4>Name: {{ $data['name'] }}</h4>
<h4>Email: {{ $data['email'] }}</h4>
</body>
</html>
Step 05: Add Route
Let’s create a web route for sending testing an email
Route::get('/send-mail',function(){
$data = [
'name'=>'NPC',
'email'=>'[email protected]'
];
\Mail::to('[email protected]')->send(new \App\Mail\SendTestMail($data));
return "Mail Sent Successfully!!";
});
Now, it’s time to run our project.
php artisan serve
Thank you for reading this article Send Email in Laravel.
Conclusion
Finally, Laravel sending mail with the help of Mailtrap is over I hope this tutorial will help you clear the concept of sending emails in the Laravel 8 application. Send Emails in Laravel using Mailtrap.
FAQ’s
Which third-party email-sending providers are supported by default in Laravel?
The following are supported by default in Laravel:
Mailgun
Sparkpost
Amazon SES
What are the common reasons why emails fail to deliver?
Reasons why emails may fail to be delivered:
A technical error in your programming script
Blocked by your hosting provider while sending
Reached the email sending rate limit set by your hosting provider
Blocked by the recipient server because of “bad reputation” of your hosting provider server
Went to SPAM or another automatically filtered folder.
How can we check if emails have been sent in Laravel?
In general, Mail::failures() returns an array of failed email addresses.
How to use mail() in Laravel?
Laravel provides a powerful and clean API over the SwiftMailer library with drivers for Mailgun, SMTP, Amazon SES, SparkPost, and send an email. With this API, we can send emails on a local server as well as the live server.
Example:
Laravel allows us to store email messages in our views files. For example, to manage our emails, we can create an email directory within our resources/views directory.
How do I create an email template in Laravel?
An email template file is a blade file that you can customize to add details and custom sections as per your email template design.