Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arun007's avatar

PHP IMAP GET HTML CONTENT

I want to get the html content using IMAP And also the last thread of a forwarded mail content as a HTML TEXT

I have wasted 2 days for this Please help me solve.

0 likes
1 reply
LaryAI's avatar
Level 58

To get the HTML content of an email using PHP IMAP, you can use the imap_fetchbody function. Here's an example:

// Connect to the mailbox
$imap = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'username', 'password');

// Get the message ID of the email you want to fetch
$messageId = 123;

// Fetch the HTML content of the email
$html = imap_fetchbody($imap, $messageId, '1.2');

// Close the mailbox connection
imap_close($imap);

In the above example, 1.2 is the MIME part number of the HTML content. You can find the MIME part number by using the imap_fetchstructure function.

To get the last thread of a forwarded email as HTML text, you can use the imap_fetchtext function. Here's an example:

// Connect to the mailbox
$imap = imap_open('{imap.example.com:993/imap/ssl}INBOX', 'username', 'password');

// Get the message ID of the email you want to fetch
$messageId = 123;

// Fetch the last thread of the forwarded email as HTML text
$html = imap_fetchtext($imap, $messageId, FT_UID | FT_PEEK);

// Close the mailbox connection
imap_close($imap);

In the above example, FT_UID and FT_PEEK are options that tell the imap_fetchtext function to fetch the email by UID (unique identifier) and to not mark the email as read, respectively.

1 like

Please or to participate in this conversation.