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

mushzak's avatar

How to rename invoice.pdf? Spark 3 on Laravel 5.3

Custom attachment name for invoice email possible?

I want to include the invoice_id in the filename invoice.pdf to get something like invoice_123.pdf but without changing core files. I use Spark 3 on Laravel 5.3.

My Listener: class InvoiceGenerated { /** * Create the event listener. * * @return void */ public function __construct() { // }

/**
 * Handle the event.
 *
 * @param  MessageSending  $event
 * @return void
 */
public function handle(MessageSending $event)
{
    $children = $event->message->getChildren();


    if(env('EMAIL_INVOICE_BCC') && $event->message->getSubject() == "** Invoice"){
        $children[0]->setFileName('gag.pdf'); // Here I want to set Invoice_id 
        $event->message->setBcc(env('EMAIL_INVOICE_BCC'));

}

}

0 likes
2 replies
Cronix's avatar

First, never use env() except withing config() files. You won't be able rely on env() calls if you cache your config, which is highly recommended in production. If the config is cached, all calls to env() will return null. You should use the config() helper to retrieve the value, not env(). Please see the warnings here: https://laravel.com/docs/5.7/configuration#configuration-caching

I don't think you can get what you're after at this point. You're hooking into the MessageSending mail event, which will probably only contain the actual email data (name, subject, attachments, etc), not the raw invoice data. You'll need to override where Spark is actually creating the invoice pdf and sending the email. Here's one way that looks like it would work: https://laracasts.com/discuss/channels/spark/add-invoice-table-information-to-pdf-invoice-without-hacking-vendor-files

mushzak's avatar

Cronix thank you for your responce, but I need to change it in email, not just dowlnoad invoice. thanks.

Please or to participate in this conversation.