簡體   English   中英

如何在PHP中使用IMAP函數來獲取HTML模式下的郵件正文內容?

[英]How to use IMAP function(s) in PHP to get the mail body content in HTML mode?

摘要

我曾嘗試使用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模式下獲取電子郵件內容嗎?

這是我用來從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/

暫無
暫無

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

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