Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"date" header is not reliable #63

Closed
perk11 opened this issue Dec 25, 2014 · 8 comments
Closed

"date" header is not reliable #63

perk11 opened this issue Dec 25, 2014 · 8 comments
Milestone

Comments

@perk11
Copy link

perk11 commented Dec 25, 2014

Sometimes date header is not in a proper format, for example, mail.ru returns this for date header: =?utf-8?B?0fAsIDQg4OLjIDIwMTAgMTM6MDE6NDAgKzAzMDA=?=
This makes library fail in file Header.php on line 33. Using maildate from headers array instead solved it for me, but I'm not sure if this is a right way to do this.

@ddeboer
Copy link
Owner

ddeboer commented Jan 23, 2015

Has this been fixed in 49b62f0?

@ddeboer ddeboer added this to the 1.0 milestone Jan 30, 2015
@ddeboer ddeboer closed this as completed Feb 1, 2015
@KundKMC
Copy link

KundKMC commented Nov 29, 2018

Hey guys, no it has not been fixed becaus I ran into the same issue today using the latest release. $message->getDate() returned null because ONLY maildate is set

$dateHeader = $this->getHeaders()->get('date');

@Slamdunk
Copy link
Collaborator

Can you post here the original raw message that gets you this behavior please?

@Slamdunk
Copy link
Collaborator

Well in this email the Date header misses completely, there is no maildate header neither.

How could your fix KundKMC@2a7fb4a work?

@KundKMC
Copy link

KundKMC commented Nov 29, 2018

You are right, there is not Header Field. But this code gets the job done:


try {
                $date = $message->getDate();
            } catch (\Throwable $ex) {
                die("FAIL:(");
            }            
if ($date == null) {
                $alteredValue = $message->getHeaders()->get('maildate');
                $alteredValue = \str_replace(',', '', $alteredValue);
                $alteredValue = \preg_replace('/^[a-zA-Z]+ ?/', '', $alteredValue);
                $alteredValue = \preg_replace('/ +\(.*\)/', '', $alteredValue);
                $alteredValue = \preg_replace('/\bUT\b/', 'UTC', $alteredValue);
                if (0 === \preg_match('/\d\d:\d\d:\d\d.* [\+\-]\d\d:?\d\d/', $alteredValue)) {
                    $alteredValue .= ' +0000';
                }
                try {
                    $date = new \DateTimeImmutable($alteredValue);
                } catch (\Throwable $ex) {
                    throw new InvalidDateHeaderException(\sprintf('Invalid Date header found: "%s"', $dateHeader), 0, $ex);
                }
            }

$date is 2013-06-19 14:49:55 with that E-Mail.
Maybe the field is set by the so called "fix" from 2015?

@Slamdunk
Copy link
Collaborator

The only possibility for your snippet to work is the presence of a Maildate header.

I can only help with the original raw message 😞

@KundKMC
Copy link

KundKMC commented Nov 29, 2018

Okay. I put some offort in it and it seems to work both ways:

mail.eml.gz

$mbox = "TESTCASE" . time();
$imap->createMailbox($mbox);
$mailbox = $imap->getMailbox($mbox);
$mailbox->addMessage(file_get_contents("/tmp/mail.eml"));
$messages = $mailbox->getMessages();
foreach ($messages as $message) {
    try {
        $date = $message->getDate();
    } catch (\Throwable $ex) {
        $date = new DateTimeImmutable("2009-01-02T00:00:00+0100");
    }
    if ($date == null) {
        var_dump($message->getHeaders()->get('maildate'));
    }
}

Creates a mailbox, uploads the EML File and shows:

string(26) "29-Nov-2018 15:46:40 +0100"

WTF!? It seems that there is some ReWriting going on, even while creating a message.

@Slamdunk
Copy link
Collaborator

Yup, it seems there an overwrite. It is likely that it happens only when querying the server through IMAp protocol, and the email remains untouched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants