0

I want a very reliable way of retrieving data from email headers. Should I use an email parser like eg https://github.com/plancake/official-library-php-email-parser or are php imap functions sufficient to get that data? What is best practice? Is there any experience? I can imagine that there is a lot of differences in formatting email headers composed by many email clients. That is why I want a reliable solution.

4

1 に答える 1

1

メールを日付で処理する必要があり、実際には他に何も必要のないプロジェクトに、組み込みの IMAP 関数を使用しました。以下のコードを使用して、それらが役立つかどうかを試してみることができます。

<?php

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user@gmail.com';
$password = 'pass123';

/* try to connect */
$inbox = imap_open($hostname, $username, $password, OP_READONLY,1) or die('Cannot connect to Gmail: ' . print_r(imap_last_error()));
$emails = imap_search($inbox, 'ALL');

/* if emails are returned, cycle through each... */
if ($emails) {

    foreach ($emails as $email_number) {

        echo imap_fetchbody($inbox, $email_number, 0);
        echo imap_fetchbody($inbox, $email_number, 1);
        echo imap_fetchbody($inbox, $email_number, 2);

    }

}

imap_close($inbox);
?>

トリッキーな部分はimap_fetchbody($inbox, $email_number, 0). この部分はヘッダーを返します。それらを取得したら、必要に応じて解析または使用できます。

それが役に立てば幸い。

于 2013-03-27T00:03:59.793 に答える