使用PHP和IMAP从收件箱中提取电子邮件地址列表

3
有人做过这个任务吗?这应该是很常见的。 我需要从一个特定的收件箱中提取所有的电子邮件地址,可以通过imap访问。这是为了营销目的(SMB,mailchimp)。这些地址可能在“发件人”或正文中的任何地方。 奇怪的是,我没有找到类似的东西,除了这些链接: Extract body text from Email PHP https://gist.github.com/AikChun/8305789 对于此任务,此链接也非常有用: http://www.php.net/manual/en/function.imap-headerinfo.php#98809 所以我现在正在编写脚本。我将把它放在github和我的网站http://lycenok.com上。 祝好,Eugene。

看看@dlo在https://dev59.com/o3A75IYBdhLWcg3wK1wp中使用imap_fetchheader的好例子。我认为使用https://gist.github.com/AikChun/8305789,你可以实现你的标准。 - Ersin Demirtas
我知道如何做。我没有找到现成的解决方案。这是我的问题。你的负评让我不想分享我的脚本。我相信它对其他人会有用的)。请再考虑一下并取消你的负评。 - Eugene Lycenok
感谢您。谢谢! - Eugene Lycenok
1个回答

3

好的,我已经写好了脚本。你可以在这里测试它: http://lycenok.com/inbox_email_list_extractor/extract_email_list_from_inbox.php

    <form method="GET">
    <p>Please, enter IMAP mailbox address, login and password (prefilled for an example)</p>
    <h2>Examples of some IMAP servers (must enable IMAP in settings)</h2>
    <ul>
    <li>Google: {imap.gmail.com:993/imap/ssl}</li>
    <li>Yandex: {imap.yandex.com:993/imap/ssl}</li>
    </ul>
    <b>Script does NOT save any provided information</b>
    <table>
    <tr><td>
    <input type="text" name="mailbox" value="<?=(isset($_REQUEST['mailbox']) ? $_REQUEST['mailbox'] : '{imap.gmail.com:993/imap/ssl}')?>"></input>
    </td></tr>
    <tr><td>
    <input type="text" name="login" value="<?=(isset($_REQUEST['login']) ? $_REQUEST['login'] : 'phpcurllogin')?>"></input>
    </td></tr>
    <tr><td>
    <input type="password" name="password" value="<?=(isset($_REQUEST['password']) ? $_REQUEST['password'] : 'phpcurllog')?>"></input>
    </tr></td>
    <tr><td>
    <tr><td>
    <input type="text" name="maxcount" value="<?=(isset($_REQUEST['maxcount']) ? $_REQUEST['maxcount'] : '10')?>"></input>
    </tr></td>
    <input type="submit" value="Get emails!"></input>
    </td></tr>
    </table>
    <input name="submitFlag" type="hidden" value="1"></input>
    </form>

    <?php
    if (isset($_REQUEST['submitFlag'])) { 

    define("MAX_EMAIL_COUNT", $_REQUEST['maxcount']);

    /* took from https://gist.github.com/agarzon/3123118 */
    function extractEmail($content) {
        $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
        preg_match_all($regexp, $content, $m);
        return isset($m[0]) ? $m[0] : array ();
    }

    function getAddressText(&$emailList, &$nameList, $addressObject) { 
        $emailList = '';
        $nameList = '';
        foreach ($addressObject as $object) {
            $emailList .= ';';
            if (isset($object->personal)) { 
                 $emailList .= $object->personal;
            } 
            $nameList .= ';';
            if (isset($object->mailbox) && isset($object->host)) { 
                $nameList .= $object->mailbox . "@" . $object->host;
            }    
        }    
        $emailList = ltrim($emailList, ';');
        $nameList = ltrim($nameList, ';');
    } 

    function processMessage($mbox, $messageNumber) { 
        echo $messageNumber;
        // get imap_fetch header and put single lines into array
        $header = imap_rfc822_parse_headers(imap_fetchheader($mbox, $messageNumber));
        $fromEmailList = '';
        $fromNameList = '';
        if (isset($header->from)) { 
            getAddressText($fromEmailList, $fromNameList, $header->from); 
        }
        $toEmailList = '';
        $toNameList = '';
        if (isset($header->to)) {
            getAddressText($toEmailList, $toNameList, $header->to); 
        }    
        $body = imap_fetchbody($mbox, $messageNumber, 1);
        $bodyEmailList = implode(';', extractEmail($body));
        print_r(
           ',' . $fromEmailList . ',' . $fromNameList 
            . ',' . $toEmailList . ',' . $toNameList 
            . ',' . $bodyEmailList . "\n"
        );
    } 

    // imap_timeout(IMAP_OPENTIMEOUT, 300);

    // Open pop mailbox
    if (!$mbox = imap_open($_REQUEST['mailbox'], $_REQUEST['login'], $_REQUEST['password'])) {
      die('Cannot connect/check pop mail! Exiting');
    }

    if ($hdr = imap_check($mbox)) {
      $msgCount = $hdr->Nmsgs;
    } else {
      echo "Failed to get mail";
      exit;
    }

    echo "<pre>";
    echo 'emails count=' . $msgCount . "\n\n\n";
    echo "record number,from emails list,from names list,to emails list, to names list,extracted from body\n";

    /* might improve performance according to
       http://www.php.net/manual/en/function.imap-headerinfo.php#98809 
       imap_headers($mbox);
    */

    for ($X = 1; $X <= min($msgCount, MAX_EMAIL_COUNT); $X++) {
        processMessage($mbox, $X);
    } 
    echo "</pre>";

    imap_close($mbox);
    }
    ?>

1
我刚刚偶然发现了这个。非常感谢你把这段代码放在这里!它刚刚为我节省了一整天的搜索时间。 - I_ATE_YOUR_WORK_FILES
1
刚在我的 Web 应用程序中测试了一下,它运行得非常好,无法感谢你够多啊,老兄。 - I_ATE_YOUR_WORK_FILES
1
由于某些原因,我无法在自己的服务器上运行脚本。这个脚本需要一些默认情况下未开启的PHP模块吗?另外,我想知道如何让脚本循环遍历所有imap文件夹,而不仅仅是收件箱。也许可以使用imap_list函数? - manicmarvin
@manicmarvin,我不记得安装任何特殊的PHP模块。 https://www.php.net/manual/en/function.imap-open.php 看起来像是一个标准的PHP函数。 出现了什么错误? - Eugene Lycenok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接