如何在PHP中使用IMAP来获取邮件正文内容? [英] How to use IMAP in PHP to fetch mail body content?

查看:812
本文介绍了如何在PHP中使用IMAP来获取邮件正文内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取电子邮件正文内容.

I can't fetch email body content.

这是我的代码

<?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);
?>

当我用来从Gmail提取电子邮件时,会显示电子邮件正文内容,但是一旦我使用邮件服务器,就无法提取电子邮件的正文内容.

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.

您能帮我解决这个问题吗?

Can you help me fix this problem?

推荐答案

我找到了解决方案, 这行有错误

I had found solution, Error is with this line

$message = imap_fetchbody($inbox,$email_number,2);

现在,我正在使用

$message = imap_fetchbody($inbox,$email_number, 1.2);

以文本/html格式接收正文内容

To receive body content in text/html format

下面,我给出了可用选项的说明.这可能是一些

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)

这篇关于如何在PHP中使用IMAP来获取邮件正文内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆