phphtmllaravelimapattachment

How to visualize and download attachments in html or php using getcontent?


I have a webpage in laravel with an email, I receive the contents of the e-mail with ddeboer IMAP Library.

To do that I have the following code:

<div class="col-md-12">
    <div class="card card-primary card-outline">
        <h5 class="card-header">
            Remetente: {{$message->getFrom()->getAddress()}}
        </h5>
        <div class="card-body p-0">
            <div class="mailbox-read-info">
                <h5> Assunto: {{$message->getSubject()}}</h5>
                <span class="mailbox-read-time float-right"> {{$message->getDate()->format('d/m/Y')}}</span></h6>
            </div>
        </div>

        <div class="mailbox-read-message">
            <h5>Conteudo: </h5>
            <p> {!! $message->getBodyHtml() !!}</p>
        </div>
    </div>
    <div class="card-footer bg-white">
        <ul class="mailbox-attachments d-flex align-items-stretch clearfix">
            @foreach ($attachments as $attachment)

                <li>
                    <span class="mailbox-attachment-icon"><i class="far fa-file-pdf"></i></span>
                    <p>{{ $attachment->getFilename() }}</p>
                    <div class="mailbox-attachment-info" type="button">
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#attach">
                            Abrir
                        </button>
                            <a class="btn btn-default btn-sm float-right">
                                <i class="fas fa-cloud-download-alt"></i></a>
                    </div>
                </li>
            @endforeach
        </ul>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="attach" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">PDF</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

This library gives me the option of downloading the attachments using:

$attachments = $message->getAttachments();

foreach ($attachments as $attachment) {
    // $attachment is instance of \Ddeboer\Imap\Message\Attachment
}

file_put_contents(
    '/my/local/dir/' . $attachment->getFilename(),
    $attachment->getDecodedContent()
);

But I have not found a way to implement this that works out, neither for downloading it, netiher open it in the modal, like with an iframe of an object.

Since all files will be either images or pdfs I think the point is download the file as .pdf and use the content, but the file or does not download, or the page does not load, or the file comes out corrupted. The console does not give associated errors either.

What is the best solution here?

I am getting this output, but I don´t think is related with the issue.

enter image description here


Solution

  • For your downloading attachment problem, you are simply not doing anything inside of the attachments forloop instead it should look something like:

    //Put this in the head of the Controller
    use Illuminate\Support\Facades\Storage;
     
    
    $attachments = $message->getAttachments();
    
    foreach ($attachments as $attachment) {
        // $attachment is instance of \Ddeboer\Imap\Message\Attachment
        
        Storage::put($attachment->getFilename(), $attachment->getDecodedContent());
    }
    

    Please check File Storage documentations here for extra options: https://laravel.com/docs/9.x/filesystem#storing-files Since you will have many attachments/folder/files

    I recommend that you put the attachments in specific folders as needed.