Home > Software engineering >  How to visualize and download attachments in html or php using getcontent?
How to visualize and download attachments in html or php using getcontent?

Time:12-26

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 >
    <div >
        <h5 >
            Remetente: {{$message->getFrom()->getAddress()}}
        </h5>
        <div >
            <div >
                <h5> Assunto: {{$message->getSubject()}}</h5>
                <span > {{$message->getDate()->format('d/m/Y')}}</span></h6>
            </div>
        </div>

        <div >
            <h5>Conteudo: </h5>
            <p> {!! $message->getBodyHtml() !!}</p>
        </div>
    </div>
    <div >
        <ul >
            @foreach ($attachments as $attachment)

                <li>
                    <span ><i ></i></span>
                    <p>{{ $attachment->getFilename() }}</p>
                    <div  type="button">
                        <button type="button"  data-toggle="modal" data-target="#attach">
                            Abrir
                        </button>
                            <a >
                                <i ></i></a>
                    </div>
                </li>
            @endforeach
        </ul>
    </div>
</div>

<!-- Modal -->
<div  id="attach" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div  role="document">
        <div >
            <div >
                <h5  id="exampleModalLabel">PDF</h5>
                <button type="button"  data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div >
                <p></p>
            </div>
            <div >
                <button type="button"  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

CodePudding user response:

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.

CodePudding user response:

I use it like this?

  <div >
    <ul >
        @foreach ($attachments as $attachment)
            <li>
                <span ><i ></i></span>
                <p>{{ $attachment->getFilename() }}</p>
                <div  type="button">
                    <button type="button"  data-toggle="modal" data-target="#attach">
                        Abrir
                    </button>
                    <a >
                            <?php
                                 Storage::put($attachment->getFilename(), $attachment->getDecodedContent());
                            ?>
                        <i ></i></a>
                </div>
            </li>
   

 @endforeach

</ul>

</div>
  • Related