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. 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.
[php]
$msgStructure = imap_fetchstructure($oIMAP, $msgNumber);
if(!empty($msgStructure->parts)) {
for($i=0, $j=count($msgStructure->parts); $i<$j; $i++) {
$part = $msgStructure->parts[$i];
if($part->subtype == “HTML”) {
$msgContent = imap_fetchbody($oIMAP, $msgNumber, $i+1);
}
}
} else {
$msgContent = imap_body($oIMAP, $msgNumber);
}
[/php]
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. π
PHP is like a foreign language to me!
we all start out one day in one language or another Chris!! Before you knew ASP, it was a foreign language to you too. Same thing happens with everyone!! I knew ASP long before PHP(& now for the past year I’ve hardly coded any ASP, just PHP). Its just a matter of when you take the plunge & dive in!! π