26
Laravel Mailtrap Setup
How will this email look? Will it look nice on mobile? Is the email data correct? You can test all that and more with Mailtrap.
Go to https://mailtrap.io/ and sign up.
Once you are logged in you will see a menu with 3 options; Inboxes, API, and Billing. Go to Inboxes.
On the Inboxes page you will see a list of your inboxes and you will also have the option of creating a new project or creating a new inbox. We will use the default inbox for this setup.
Click on the inbox and on SMTP Settings select Integrations -> Laravel.
- Here you will see the .env variables you need to copy.
Add (or replace) mail environment variables from Mailtrap to your application .env file.
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=YOUR_INBOX_USERNAME
MAIL_PASSWORD=YOUR_INBOX_PASSWORD
MAIL_ENCRYPTION=tls
# Also
[email protected]
MAIL_FROM_NAME="Mailtrap Test App"
Create simple test mailable with markdown.
php artisan make:mail MailtrapTest --markdown=emails.test
App\Mail\MailtrapTest.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MailtrapTest extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.test');
}
}
resources\views\emails\test.blade.php (Markdown file)
@component('mail::message')
# TEST EMAIL
The cake is a lie
Thanks,<br>
{{ config('app.name') }}
@endcomponent
Add test route
...
...
Route::get('/mailtrap/test', function () {
// Using a temp email.
// This way we can test that emails are really trapped.
$testEmail = '[email protected]';
return Mail::to($testEmail)->send(new MailtrapTest());
});
...
...
Now hit test URL. You should have a new email on your Mailtrap inbox and you should not have any email from your application on the test email inbox.
Mailtrap is a very useful tool for development and it's super easy to setup with Laravel. You can find more information at https://mailtrap.io/blog/send-email-in-laravel/
I hope this article was helpful.
If you have any feedback or found mistakes, please don’t hesitate to reach out to me.
26