Search code examples
phpimapemail-attachmentsgmail-imap

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: https://github.com/barbushin/php-imap

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');
require_once('php_imap/IncomingMail.php');
require_once('php_imap/Mailbox.php');
//require_once('PHPMailer/class.phpmailer.php');

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

$mailbox_name = "[email protected]";
$mailboxPassword = "xxxxxxxxxxxx";

// Accessing the mailbox
$mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl/novalidate-cert}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 />";
    if($mail->cc){
       echo "CC: " . $mail->cc . "<br />";
    }
    echo "Subject: " . $mail->subject . "<br /><br />";
    if($mail->textHtml == NULL){
        echo $mail->textPlain;
    }else{
        echo $mail->textHtml;
    }
    echo "<br /><br />";
    echo "---------------------------End of Email ---------------------------";
    echo "<br /><br /><br /><br /><br /><br />";

    echo "attachments: ";
    echo "<PRE>";
    var_dump($mail->getAttachments());
    echo "</PRE>";
}

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

attachments:

array(3) {
  ["[email protected]"]=>
  object(PhpImap\IncomingMailAttachment)#21 (3) {
    ["id"]=>
    string(30) "[email protected]"
    ["name"]=>
    string(12) "image002.jpg"
    ["filePath"]=>
    NULL
  }
  ["[email protected]"]=>
  object(PhpImap\IncomingMailAttachment)#12 (3) {
    ["id"]=>
    string(30) "[email protected]"
    ["name"]=>
    string(12) "image004.png"
    ["filePath"]=>
    NULL
  }
  ["[email protected]"]=>
  object(PhpImap\IncomingMailAttachment)#32 (3) {
    ["id"]=>
    string(30) "[email protected]"
    ["name"]=>
    string(12) "image005.jpg"
    ["filePath"]=>
    NULL
  }
}

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?


Solution

  • 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.