Retrieving eMail Message through PHP!!
In the past week, I was messing around with the imap functions of PHP, to connect to a POP3 server, get eMail messages, etc.
But the problem is that imap_body() function doesn't give out just the email message if the eMail is a multipart message. :think: I was having problems with this as I didn't find any solution in the comments on the online manual's page & what I was trying to do is to get the HTML from an HTML eMail, but I got the plain text part as well as the headers if I used imap_body() for a multipart HTML eMail.
So I tried to get around it by looking at the eMail structure using the imap_fetchstructure(). And I ended up with the following code doing the job for me.
-
$msgStructure = imap_fetchstructure($oIMAP, $msgNumber);
-
$part = $msgStructure->parts[$i];
-
if($part->subtype == "HTML") {
-
$msgContent = imap_fetchbody($oIMAP, $msgNumber, $i+1);
-
}
-
}
-
} else {
-
$msgContent = imap_body($oIMAP, $msgNumber);
-
}
This piece of code will check if the message is multipart or not. If its multipart, then it'll get the HTML message for you else you'll get the TEXT message.
Now $oIMAP is the IMAP connection object that you will create & $msgNumber is ofcourse the number of the eMail whose message you want to get.
This piece of code is not a full solution or any function etc. Its just a code snippet which is meaningless by itself but which you can use if you work with POP3 in your PHP scripts. ![]()
Please leave a Comment
If you would like to make a comment, please fill out the form below.