0
votes

I have a specific request to connect to local (eml) file as imap server. It has been done in the past so i know this can be achieved. My code is similar to the following:

<?php

// remember original $HOME environment variable so we can set it back once we're done
$home_path = getenv('HOME');

// this is needed so we can access local .eml file as mailbox
putenv("HOME=/var/www/test/"); 

// mailbox.eml is in inside /var/www/test/ which is relative to the imap at the moment
$stream = imap_open('mailbox.eml', '', ''); 

// check the mailbox 
$MC = imap_check($stream);

// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($stream,"1:{$MC->Nmsgs}",0);

var_dump($result);exit;

this var_dump($result) produces the following output:

array(1) {
  [0]=>
  object(stdClass)#87 (13) {
    ["subject"]=>
    string(52) "/var/www/test/mailbox.eml"
    ["from"]=>
    string(20) "User-Number-501@test"
    ["date"]=>
    string(31) "Fri, 20 Nov 2015 14:16:06 +0000"
    ["size"]=>
    int(2696)
    ["uid"]=>
    int(1)
    ["msgno"]=>
    int(1)
    ["recent"]=>
    int(1)
    ["flagged"]=>
    int(0)
    ["answered"]=>
    int(0)
    ["deleted"]=>
    int(0)
    ["seen"]=>
    int(0)
    ["draft"]=>
    int(0)
    ["udate"]=>
    int(1448028966)
  }
}

This information about 'from' is bad. It should state and email address from which the mail has been sent. Instead, I'm getting the "User-Number-501@test".

I managed to figure out that the "501" part is a user name in my vagrant machine where this script runs on. Tried on other machine with no vagrant, and still got the user name of that machine in 'from' field.

If i connect to the real imap server, i get the correct output of this mailbox message, like this:

array(1) {
  [0]=>
  object(stdClass)#2 (15) {
    ["subject"]=>
    string(14) "test test test"
    ["from"]=>
    string(62) "=?UTF-8?B?U3LEkWFuIE1hcmphbm92acSH?= <[email protected]>"
    ["to"]=>
    string(44) "[email protected]"
    ["date"]=>
    string(31) "Thu, 19 Nov 2015 14:50:18 +0100"
    ["message_id"]=>
    string(68) "<[email protected]>"
    ["size"]=>
    int(3148)
    ["uid"]=>
    int(1)
    ["msgno"]=>
    int(1)
    ["recent"]=>
    int(0)
    ["flagged"]=>
    int(0)
    ["answered"]=>
    int(0)
    ["deleted"]=>
    int(0)
    ["seen"]=>
    int(1)
    ["draft"]=>
    int(0)
    ["udate"]=>
    int(1447944622)
  }
}

Other info seem to be "overwritten" as well.

Any ideas on what might make the imap to misbehave and how to fix this?

note: PHP 5.6.13-1+

1

1 Answers

0
votes

To answer my own question for anyone who stumble upon the problem like this: apparently, the mailbox.eml has to have a "From" as the first line in it. For example adding this line to the top of the mailbox.eml all of my problems:

From MAILER-DAEMON Tue Nov 24 10:29:08 2015