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 回答