sujit prasad
sujit prasad

Reputation: 945

PHP imap_fetchbody

I have been trying to fetch message but unsuccessful.

$body = imap_fetchbody($inbox, $email_id, 0);

the messages without attachments are good and I have output but with attachments gives some complicated outputs out of which both html and plain message are encoded with some (Content-Type) which is a part of gmail messages

Upvotes: 1

Views: 4563

Answers (3)

vaddy
vaddy

Reputation: 77

$body = imap_fetchbody($inbox, $email_id, 1.0);

this seems to be the only one working for me. I think the first integer in the last parameter represents the section of the email, so if it starts with a zero it will contain all the header information. If it starts with a one then it contains the message information. Then the second integer followed by the period is the section of that section. So when I put zero it shows information, but when I put one or two it doesn't show anything for some emails.

Upvotes: 1

sujit prasad
sujit prasad

Reputation: 945

This helped

$body = imap_fetchbody($inbox, $email_id, 1.1);

Upvotes: 0

vim
vim

Reputation: 1550

You can use the following code to get the plain text part of a multipart email body:

<?php

//get the body
$body = imap_fetchbody($inbox, $email_id, 0);

//parse the boundary separator
$matches = array();
preg_match('#Content-Type: multipart\/[^;]+;\s*boundary="([^"]+)"#i', $body, $matches);
list(, $boundary) = $matches;

$text = '';
if(!empty($boundary)) {
    //split the body into the boundary parts
    $emailSegments = explode('--' . $boundary, $body);

    //get the plain text part
    foreach($emailSegments as $segment) {
        if(stristr($segment, 'Content-Type: text/plain') !== false) {
            $text = trim(preg_replace('/Content-(Type|ID|Disposition|Transfer-Encoding):.*?\r\n/is', '', $segment));
            break;
        }
    }
}

echo $text;
?> 

Upvotes: 2

Related Questions