如何在 PHP 中使用 IMAP 来获取邮件正文内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5177772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use IMAP in PHP to fetch mail body content?
提问by Mohan Ram
I can't fetch email body content.
我无法获取电子邮件正文内容。
This is my code
这是我的代码
<?php
/* connect to server */
$hostname = '{myserver/pop3/novalidate-cert}INBOX';
$username = 'username';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error());
//echo $inbox;
/* 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) {
//$email_number=$emails[0];
//print_r($emails);
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* output the email header information */
$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '</div>';
/* output the email body */
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
When I use to fetch email from Gmail, the email body content is displayed, but once I use my mail server, I can't fetch the body content of email.
我以前从Gmail中获取邮件时,会显示邮件正文内容,但是一旦使用我的邮件服务器,就无法获取邮件正文内容。
Can you help me fix this problem?
你能帮我解决这个问题吗?
回答by Mohan Ram
I had found solution, Error is with this line
我找到了解决方案,错误出在这一行
$message = imap_fetchbody($inbox,$email_number,2);
Now, I am using
现在,我正在使用
$message = imap_fetchbody($inbox,$email_number, 1.2);
To receive body content in text/html format
以 text/html 格式接收正文内容
Below i had given datails of available options. This might some one
下面我给出了可用选项的数据。这可能是某个
()Root Message Part (multipart/related)
(1) The text parts of the message (multipart/alternative)
(1.1) Plain text version (text/plain)
(1.2) HTML version (text/html)
(2) The background stationary (image/gif)
回答by tobyS
The Zeta Mail component allows much more convenient fetching of mails from IMAPand POPand can parses the incoming emails into a nice and clean object structure so you can handle it easily.
Zeta Mail 组件允许更方便地从 IMAP和POP获取邮件,并且可以将传入的电子邮件解析为一个漂亮而干净的对象结构,以便您可以轻松处理它。