【发布时间】:2016-03-23 23:41:59
【问题描述】:
总结
我曾尝试使用imap_fetchbody 和imap_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_fetchbody 和imap_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”和邮件标题。