簡體   English   中英

如何在PHP中使用IMAP來獲取郵件正文內容?

[英]How to use IMAP in PHP to fetch mail 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提取電子郵件時,會顯示電子郵件正文內容,但是一旦我使用郵件服務器,便無法提取電子郵件的正文內容。

您能幫我解決這個問題嗎?

我找到了解決方案,此行出現錯誤

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

現在,我正在使用

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

接收text / html格式的正文內容

下面我給出了可用選項的數據。 這可能是一些

()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)

Zeta Mail組件使從IMAPPOP 提取郵件更加方便,並且可以將傳入的電子郵件解析為一個漂亮干凈的對象結構,因此您可以輕松地處理它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM