user1547410 user1547410 - 1 month ago 9x
PHP Question

barbushin imap-php get attachments how to store attachments on server

I am working with a class for parsing and viewing emails and it works great:

Now i am trying to save the attachments to the server but ive never worked with email attachments before so not sure how it works.

This is my code for echoing out the emails:

header('Content-Type: text/html; charset=utf-8');

use PhpImap\Mailbox as ImapMailbox;
use PhpImap\IncomingMail;
use PhpImap\IncomingMailAttachment;

$mailbox_name = "";
$mailboxPassword = "xxxxxxxxxxxx";

// Accessing the mailbox
$mailbox = new PhpImap\Mailbox('{}INBOX', $mailbox_name, $mailboxPassword);
$mails = array();

$mailsIds = $mailbox->searchMailBox('UNSEEN');
if(!$mailsIds) {
die('Mailbox is empty');

foreach ($mailsIds as $mailId){
$mail = $mailbox->getMail($mailId);
echo "---------------------------Start of Email ---------------------------" . "<br />";
echo $mail->date . "<br />";
echo "From: " . $mail->fromName . " - " . $mail->fromAddress . "<br />";
echo "CC: " . $mail->cc . "<br />";
echo "Subject: " . $mail->subject . "<br /><br />";
if($mail->textHtml == NULL){
echo $mail->textPlain;
echo $mail->textHtml;
echo "<br /><br />";
echo "---------------------------End of Email ---------------------------";
echo "<br /><br /><br /><br /><br /><br />";

echo "attachments: ";
echo "<PRE>";
echo "</PRE>";

Now the email content comes out fine, the attachments echoes out like this:


array(3) {
object(PhpImap\IncomingMailAttachment)#21 (3) {
string(30) "image002.jpg@01D0838E.40768C10"
string(12) "image002.jpg"
object(PhpImap\IncomingMailAttachment)#12 (3) {
string(30) "image004.png@01D0838E.40768C10"
string(12) "image004.png"
object(PhpImap\IncomingMailAttachment)#32 (3) {
string(30) "image005.jpg@01D0838E.40768C10"
string(12) "image005.jpg"

Since the path is null i am not sure if there is a problem here with my script or if thats normal but without a patch how can i save the images to my server so i can display them in an email. I done a bunch of googling but i think it might be a different application with this class and there isnt really any documentation i can find. What i am struggling with is i dont understand the concept of email attachments.

With attachments sent as $_POST i can move them from a temp folder to a permanent one because i have the path value in the $_files info however here i just have an image id and image name so i am not sure where the image actually is. Any ideas of how to take the attachment info and save the files to the server so i can display them to the user?

lmz lmz

Reading the source code: you need to specify a directory to store the attachments in.

class Mailbox {
# ....
public function __construct($imapPath, $login, $password, 
    $attachmentsDir = null, $serverEncoding = 'UTF-8')

If it is null attachments will not be saved locally, thus no filePath.