【问题标题】:how can I display the contents of an email to a website?如何将电子邮件的内容显示到网站?
【发布时间】:2012-01-11 19:05:51
【问题描述】:

我想显示电子邮件的正文内容。我在 php 中尝试过 IMAP,但有些地方非常错误。 IMAP 没有接收到我的邮件正文。它只拾取身体中的签名。因此,我正在寻找将电子邮件正文内容读取到网页的替代方法。

这是我的电子邮件的原始文件:

http://pastebin.com/WQra335P

IMAP 正在抓取免责声明/版权模糊,但正文中没有显示任何其他内容。任何人都有其他方法可以从 gmail 或任何其他可以将内容显示到网页的网站读取电子邮件?

我已经放弃让 IMAP 读取它,因为没有人能够找出问题所在...我已经花了几个小时所以我放弃了,但这里是代码...

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

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

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

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

        /* begin output var */
        $output = '';

        /* put the newest emails on top */
        rsort($emails);

        /* for every email... */
        foreach($emails as $email_number) {

            /* get information specific to this email */
            $overview = imap_fetch_overview($inbox, $email_number, 0);
            $message = imap_fetchbody($inbox, $email_number, 2);
            echo $message;
            echo imap_qprint($message);
            $message = imap_qprint($message);
            echo imap_8bit ($message);
            $DateFormatted = str_replace("-0500", "", $overview[0] -> date);

            /* output the email header information */
            $output .=  $overview[0] -> subject ;
            $output .=  $DateFormatted ;

            //$bodyFormatted = preg_replace("/This e-mail(.*)/","",$message);
            //$bodyFormatted = preg_replace("/Ce courriel(.*)/","",$bodyFormatted);
            /* output the email body */
            $output .=  $message;

        }

    echo $output;

    }

    /* close the connection */
    imap_close($inbox);

?>

【问题讨论】:

  • 你能贴出你尝试过的IMAP代码吗?

标签: php email imap


【解决方案1】:

Gmail 有一些不同的 IMAP 设置,更严格地遵循原始代码:

http://davidwalsh.name/gmail-php-imap

【讨论】:

  • 我有完全相同的代码,它不适用于我的电子邮件标题或从 Exchange 服务器发送到 gmail 帐户的电子邮件。 IMAP 除了签名什么都不能读取。但是,如果我采用相同的电子邮件内容并将它们从 gmail 或电子邮件通过电子邮件发送到 gmail 帐户,那么它确实会读取它们......这与我从交换服务器到 gmail 的电子邮件的格式或标题有关。我在上面的帖子中的 pastebin 链接上发布了哪些标题
  • Exchange 服务器是另一种野兽,它不完全符合 IMAP 标准。
  • 在这种情况下我该怎么办?总结一下 gmail-gmail 对我有用,但交换服务器-gmail 不起作用.. gmail-gmail 表示发送到 gmail 帐户的 gmail 电子邮件.. 或 yahoo-gmail 表示发送到 gmail 帐户的 yahoo 电子邮件
【解决方案2】:

您正在处理多部分消息(查看您的 pastebin 电子邮件示例)。 作为测试尝试使用此行:

$message = imap_fetchbody($inbox, $email_number, "1.1"); 

纯文本版本低于 1.1 HTML 版本是 1.2 签名在下一部分 - 它是 2。这就是您在代码示例中检索到的内容。

【讨论】:

  • 谢谢,我使用的是 "$message = imap_fetchbody($inbox, $email_number, "1.1"); ",这就是为什么我除了正文之外还获得了随机标题,添加了.1 修复了标题,但“=”仍然存在。
【解决方案3】:

您是否有权访问原始电子邮件内容(包含所有标题等的内容)

如果是这样,请尝试使用plancacke email parser

我以前使用过它并取得了很好的成功。

$emailParser = new PlancakeEmailParser(...raw email content...);

$emailTo = $emailParser->getTo();
$emailSubject = $emailParser->getSubject();
$emailCc = $emailParser->getCc();
$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');
$emailBody = $emailParser->getPlainBody();
$emailHtml = $emailParser->getHTMLBody();

【讨论】:

    【解决方案4】:

    除了 DmitryK 的建议之外,

    添加以下内容可以使一切正常,而没有随机的“=”符号。 Str_replace 用于删除在页面上生成的“=”。

    $message = imap_fetchbody($inbox, $email_number, "1.1"); 
    $message = str_replace("=", "", $message);
    

    我不知道 100% 为什么“=”是随机生成的,但这很可能是由于 Exchange Server 方面的一些加密问题,因为我们的服务器已经使用了大约 10 年。

    【讨论】:

    • 确保使用 utf-8。如果是这种情况,我建议您改用quoted_printable_decode()。它删除了“=”。否则,您还必须将其编码为 utf-8。 utf-8_encode(quoted_printable_decode());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2012-05-21
    • 2014-05-25
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多