简体   繁体   English

电子邮件解析器或PHP imap函数?

[英]email parser or php imap functions?

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? 我应该使用电子邮件解析器,例如https://github.com/plancake/official-library-php-email-parser还是php imap函数足以获取该数据? 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. 这就是为什么我想要一个可靠的解决方案。

I used built-in IMAP functions for a project which requires processing the emails by their dates and didn't need anything else actually. 我为一个项目使用了内置的IMAP函数,该函数需要按日期来处理电子邮件,实际上并不需要其他任何东西。 You can try and see if they are useful for you with the code below; 您可以使用以下代码尝试查看它们是否对您有用;

<?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);
?>

The tricky part is imap_fetchbody($inbox, $email_number, 0) . 棘手的部分是imap_fetchbody($inbox, $email_number, 0) This part will return the headers. 这部分将返回标题。 Once you get them, you can parse or use it as you wish. 一旦获得它们,就可以根据需要解析或使用它。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM