Codementor Events

Sending mail with attachment using laravel and mailtrap.io

Published Jan 21, 2020
Sending mail with attachment using laravel and mailtrap.io

Laravel provides a very simple api to send mail to users. Today in this topic I am going to show you how to send mail with attachment using mailtrap.io.

MailTrap is a tool through which we can test our outgoing mail,their look and feel on various devices. All we will be learning today.

Make laravel mailer class

In laravel we can create a mailer class through artisan command.

| php artisan make:mail SendMailWithAttachment |

As a result, it will generate a filename SendMailWithAttachment.php in App\Mail directory. Just copy paste below contents in the generated file.

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailWithAttachment extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
    * Create a new message instance.
    *
    * @return void
    */
    public function __construct()
    {
        //
    }

    /**
    * Build the message.
    *
    * @return $this
    */
    public function build()
    {
          return $this->to('test@example.com')
              ->attach(storage_path('app/1569316022.jpg'))
              ->markdown('mail_example');
    }
}

In the above we are implementing ShouldQueue interface, because I usually make use of laravel queue to send mails, because user should not wait until mail is sent, that’s why I use Queue.

Essentially, in the build() method, we are mentioning the recipient email id in to() method, in attach() method we are giving the full path of the file we are sending as an attachment, since my file is in laravel’s storage directory only that’s why I am calling the file with storage_path() helper function. Finally, I am using the markdown(), as it allows us to use mail template with custom values.

resources/views/mail_example.blade.php

@component('mail::message')

#Sending Mail With Attachment

Dear reader,
This is just a simple example of sending mail with attachment.
PFA.

<br><br>
Thanks,<br>
{{ config('app.name') }}
@endcomponent

Configuring MailTrap.io

Go to https://mailtrap.io, and register there. After successful registration, select laravel and  you will see such screen.

Mailtrap configuration for laravel

Copy the highlighted section into your .env file, there is already mailtrap set with empty values.

Testing the mail

In web.php, define a route for triggering the mailer class.

| Route::get(‘mail-with-attachment’, function (){    \Illuminate\Support\Facades\Mail::queue(new \App\Mail\SendMailWithAttachment());

});

|

Now open the terminals, and the follow commands in each terminal.

First terminal: php artisan serve

Second terminal: redis-server ( download the redis server from here )

Third terminal: php artisan queue:work

Lastly, go to http://localhost:8000/mail-with-attachment and check mailtrap.io

Testing mail with attachment in mailtrap

Hoorray, We received a mail in mailtrap.io with an attachment.

So friends, that’s all about sending mail with attachment using laravel and mailtrap.io.

Ebooks available now

You can download this article’s PDF eBooks for offline reading from below:

Discover and read more posts from Decode Web
get started