We have already seen how to send mail in Laravel 10 and how to send queue mail in Laravel 10. But in this tutorial, we will see how to send a mail with an attachment in Laravel 10 application. You will see Laravel attach file to email from storage directory from this tutorial.

I will store a pdf in laravel storage directory and we will send this pdf with email in Laravel. Laravel 10 provides attachments() method which returns attachment arrays for sending with email. We will use this attachments method to laravel send email with pdf attachment.

You can see the demo image from my mail trap inbox of this tutorial laravel mail attachment from url. See the demo pic below:

laravel-10-mail-with-attachement-example

Step 1: Install Laravel 10

In the first step, we need a fresh laravel 10 application. So open the terminal and run the below command:

composer create-project laravel/laravel example-app

 

Step 2: Make Configuration

I am going to use mail trap for testing mail with pdf attachments in Laravel 10. So configure it like:

.env

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=bcca41cf7e855f
MAIL_PASSWORD=c1abf4f6f9596d
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="laravelia@tutorial.com"
MAIL_FROM_NAME="${APP_NAME}"

 

Read also: Laravel 10 Algolia Full Text Search Tutorial

 

Step 3: Create Mail

There is an artisan command to create a mail class in Laravel. We can run make:mail command to create a mail class. Run the below command to create a mail class.

php artisan make:mail MailWithAttachment

 

Now, let's update the code on the MailWithAttachment.php file as below:

app/Mail/MailWithAttachment.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailWithAttachment extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    /**
     * Create a new message instance.
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address('laravelia@example.com', 'Mahedi Hasan'),
            subject: 'Laravel Ten Mail With Attachment',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'email.test',
            with: [
                'data' => $this->data
            ]
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [
            Attachment::fromPath($this->data['attachment'])
                ->as('laravelia.pdf')
                ->withMime('application/pdf')
        ];
    }
}

 

You can use fromStorage also like:

/**
 * Get the attachments for the message.
 *
 * @return array<int, \Illuminate\Mail\Mailables\Attachment>
 */
public function attachments(): array
{
    return [
        Attachment::fromStorage('/path/to/file')
                ->as('name.pdf')
                ->withMime('application/pdf'),
    ];
}

 

The fromStorageDisk method may be used if you need to specify a storage disk other than your default disk like below:

/**
 * Get the attachments for the message.
 *
 * @return array<int, \Illuminate\Mail\Mailables\Attachment>
 */
public function attachments(): array
{
    return [
        Attachment::fromStorageDisk('s3', '/path/to/file')
                ->as('name.pdf')
                ->withMime('application/pdf'),
    ];
}

 

Step 4: Add Routes

We are not going to create any controllers. We will send mail from the web.php file. So update the file like below:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Mail\MailWithAttachment;
use Illuminate\Support\Facades\Mail;


Route::get('mail_with_attachment', function () {

    $data = [
        'body' => "We are learning Laravel 10 mail from laravelia.com",
        'attachment' => public_path('/storage/resume.pdf')
    ];

    Mail::to('laravelia@test.com')->send(new MailWithAttachment($data));

    dd('Mail send successfully.');
});

 

Step 5: Create Email View

In this step, we will create a blade view file and write the email that we want to send. now we just echo out our dynamic message like:

resources/views/email/test.blade.php

Hello Friend <br>

<div>
    Message: {{ $data['body'] }} <br><br>

    Here is an Attachment data:
 
    {{ $message->embed($data['attachment']) }}

</div>

 

Now you can test by visiting the below URL:

URL
http://localhost:8000/mail_with_attachment

 

Read also: Create Jobs And Send Mail Via Queue In Laravel 10

 

Conclusion

Now we know laravel 10 send mail with pdf. Now we also see laravel 10 send email with attachment. Hope this laravel mail attachment from url tutorial will help you. Hope this laravel attach file to email from storage tutorial will help you.

Category : #laravel

Tags : #laravel , #laravel mail