【问题标题】:How to use IMAP function(s) in PHP to get the mail body content in HTML mode?如何在 PHP 中使用 IMAP 函数以 HTML 模式获取邮件正文内容?
【发布时间】:2016-03-23 23:41:59
【问题描述】:

总结

我曾尝试使用imap_fetchbodyimap_body 函数以HTML 模式获取电子邮件内容,但失败了。以下是我尝试过的 PHP 代码。你能告诉我错误在哪里吗?顺便说一句,邮件可能是用其他语言编写的,所以请注意字符集。

代码

imap_fetchbody:

<?php
/* connect to server */

$mail_server="mail.example.com";

$mail_link="{{$mail_server}:143/imap/notls}" ;

$mail_user="user@mail.example.com";

$mail_pass="password";

/* try to connect */
$inbox = imap_open($mail_link, $mail_user, $mail_pass) or die('Cannot connect: ' . imap_last_error());

/* email number */
$mail_number = 1;       //The number of the mail (The first mail)

/* fetch the email content in HTML mode */
$message = imap_fetchbody($inbox,$mail_number,1.2);

/* output the email body */
$output= '<div class="body">'.$message.'</div>';

echo $output;

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

imap_body:

<?php

/* connect to server */
$mail_server="mail.example.com";

$mail_link="{{$mail_server}:143/imap/notls}" ;

$mail_user="user@mail.example.com";

$mail_pass="password";

/* try to connect */
$inbox = imap_open($mail_link, $mail_user, $mail_pass) or die('Cannot connect: ' . imap_last_error());

/* email number */
$mail_number = 1;       //The number of the mail (The first mail)

/* fetch the email content in HTML mode */
$message = imap_qprint(imap_body($inbox,$mail_number));

/* output the email body */
$output= '<div class="body">'.$message.'</div>';

echo $output;

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

后记

除了imap_fetchbodyimap_body 之外,还有没有其他IMAP 函数也可以在HTML 模式下获取电子邮件内容?

【问题讨论】:

  • 你能定义failed吗?你有错误吗?没有?出乎意料的事情?僵尸末日?
  • @giorgio 如imap_fetchbody,它没有在屏幕上显示任何结果。而imap_body,虽然我已经努力摆脱那些“mojibake”,但它仍然在屏幕上显示了一些“mojibake”(乱码)。
  • 啊,这听起来像字符编码。也许是Base64?还是utf8?您可以张贴邮件的标题(如果您不想收到垃圾邮件,请务必删除您的电子邮件地址;))?还有mojibake的样品? (其实我喜欢这个词;))
  • @giorgio A:i.imgur.com/P4T3pkU.jpgB:i.imgur.com/4czDlhs.jpgA是邮件正文内容的原始输出结果。很清楚,没有看到任何“mojibake”。 B 是我使用 PHP 函数 imap_body 获取邮件内容的 PHP 页面的屏幕截图。您可以在“真实”HTML 内容的上方和下方看到如此多的“mojibake”和邮件标题。

标签: php email imap


【解决方案1】:

这是我用来从 gmail 抓取电子邮件并阅读它们的一些代码:

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

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

/* if emails are returned, cycle through each... */
if ($emails)
{
    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach ($emails as $email_number)
    {
        $emailMessage = new EmailMessage($inbox, $email_number);
        $emailMessage->fetch();

        $dom = new DOMDocument;
        libxml_use_internal_errors(true);
        $dom->loadHTML($emailMessage->bodyHTML);
    }
}

EmailMessage 类可以在这里找到:http://www.electrictoolbox.com/php-email-message-class-extracting-attachments/

【讨论】:

    猜你喜欢
    • 2011-07-07
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多