Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matttonks11's avatar

Laravel mail & attachData()

Hi,

I'm working on adding attachments to some mail I'm sending and I was wondering how you specify the file name if you don't know the file extension.

For example if I want to allow users to send excel files and pdfs, how would I create the file name based on it's extension? if that makes sense?


    public function build()
    {
        return $this->view('emails.orders.shipped')
                    ->attachData(
                $this->pdf, 
                'name.pdf' // could be one of many file types 
            );
    }

0 likes
2 replies
D9705996's avatar
D9705996
Best Answer
Level 51

The attachDataarguements accepts the raw data for the attachment, the filename and an array of options, such as mine type.

https://laravel.com/docs/5.7/mail#attachments

Therefore if you want to be able change the attachment type you just need to pass in the correct values

->attachData(
   $this->excel, 
   'name.xls'
);

It's up to you how you generate the raw data and filename as laravel won't do this for you.

1 like

Please or to participate in this conversation.