【问题标题】:Most efficient way to save POP (Gmail) emails to MySQL database [closed]将 POP(Gmail)电子邮件保存到 MySQL 数据库的最有效方法 [关闭]
【发布时间】:2014-10-17 07:33:21
【问题描述】:

我需要对从 Gmail 收到的电子邮件进行查询。在运行 Google Business Apps 时,我可以通过 POP 和 IMAP 访问它。为了避免 API 的使用,我对简单地将电子邮件下载到 MySQL 数据库感兴趣。最后,我将展示 CRM 的使用统计数据和指标,但现在我只需要定期下载它们(每 5 分钟一次)。

附加信息:我使用的是 Ubuntu 14.04。我的应用程序将使用 PHP 构建,但为了简单起见,我可以使用 bash 或 Python 脚本获取所有电子邮件。在这一点上我并不挑剔。

我遇到了一些非常过时的 PHP 脚本,但我想知道是否有更新的内容:

http://www.phpclasses.org/package/3324-PHP-Retrieve-e-mail-messages-into-a-MySQL-database.html http://www.phpclasses.org/package/2-PHP-Access-to-e-mail-mailboxes-using-the-POP3-protocol.html

附:我打算使用像 WebMail Lite 或 Roundcube 这样的开源电子邮件客户端,但他们似乎没有将电子邮件保存到数据库中,尽管我认为他们实际上可能使用了一个 API,这可能让我获得我需要的统计信息但同样,我在想会有一个更有效的解决方案。

【问题讨论】:

    标签: php python mysql bash email


    【解决方案1】:

    对于这么简单的事情,您不需要整个库。

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'myemail@gmail.com';
    $password = 'mypassword';
    $inbox = imap_open($hostname,$username,$password);
    $emails = imap_search($inbox,'ALL');
    foreach($emails as $e){
        $overview = imap_fetch_overview($inbox,$e,0);
        $message = imap_fetchbody($inbox,$e,2);
        // the body of the message is in $message
        $details = $overview[0];
        // you can do a var_dump($details) to see which parts you need
        //then do whatever to insert them into your DB
    }
    

    【讨论】:

    • 哇,这非常简单!似乎还有一个 imap_search 函数可以让我缩小结果范围并进行一些基本计算。如果是这种情况,我可以在技术上即时执行所有指标,而不是将它们保存到数据库中。谢谢阿德尔菲亚!
    • 绝对是,它直接在foreach之前使用,您可以使用任何搜索条件,在我的示例中,我使用“ALL”来返回所有电子邮件。
    • 我收到错误“未知:在 [ALERT] 后重试 PLAIN 身份验证”
    猜你喜欢
    • 2023-03-06
    • 2021-09-12
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多