I am trying to send an email with a pdf file attached to it. I checked and the file does exist at the path. I can open it aswell. I tested if i can download the file with the download function of the storage facade and that also worked.
However when i try it in a queued email, it fails everytime after waiting about 20 seconds. This is the error i got:
ValueError: fopen(): Argument #1 ($filename) must not contain any null bytes in C:\Users\Gebruiker\PhpstormProjects\FuegoWebsite\vendor\swiftmailer\swiftmailer\lib\classes\Swift\ByteStream\FileByteStream.php:129
Stack trace:
And my email code is:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
class PdfTestMail extends Mailable
{
use Queueable, SerializesModels;
public $orderId;
public $text;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($orderid, $text)
{
$this->orderId = $orderid;
$this->text = $text;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$file = Storage::disk('private')->get("factuur_order_".$this->orderId.'.pdf');
return $this->text('emails.notifyAdmin')
->subject('Orderbevestiging #'.$this->orderId)
->attach($file, [
'as' => 'factuur.pdf',
'mime' => 'application/pdf'
]);
}
}
I tried to attach the pdf in multiple ways, including the direct output of the barryvdh/dompdf package i use to generate a pdf. nothing works and i have no idea why.
The attach
method takes a filename for the file to attach, not data. You are probably looking for the attachData
method:
public function attachData($data, $name, array $options = [])
To switch to attachData
:
->attachData($file, 'factuur.pdf', ['mime' => 'application/pdf'])
Laravel 8.x Docs - Mail - Attachments - Raw Data Attachments attachData