Reputation: 1026
I have been trying to move from directly sending emails, to adding them to Laravel Queue
, and I've noticed that if the email contains any files, it does not really like to deal with it.
For example, this example works fine if I was to send the email directly, but it will not if I want to add it to the Queue
because it will not serialise the $file
variable.
$this->mail->queue('emails.contact-us',compact('data'), function($message) use($files)
{
$message->from('[email protected]', 'Test Subject');
foreach($files as $file){
$message->attach($file->getRealPath(),array(
'as' => $file->getClientOriginalName(),
'mime' => $file->getMimeType()
));
}
});
If I tried to getFileContents
, store all data to a varaible and pass the variable to the Queue
, it will create a Job
, however instead of the Json
payload data that it should produce, it adds 0
to the payload
column.
So my question is, has anybody dealt with this situation before? I have thought about adding file temporary, but it does not seem like a good solution.
Upvotes: 3
Views: 2292
Reputation: 118
You can use attachData, just pass the file data and the name into it
$message->attachData($data, $name, array $options = []);
Go take a look at this for more in depth info : https://laracasts.com/discuss/channels/laravel/sending-email-with-a-pdf-attachment
EDIT: Try a Base64 encoding
$this->fileContents = base64_encode(File::get($this->file));
$this->attachData(base64_decode($this->fileContents), 'file.pdf');
Upvotes: 4