简体   繁体   中英

Laravel Queue Mail files

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@mail.co.uk', '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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM