I have mails where I need to obtain the body via PHP imap functions and then storing it in the database. These mails a send from a response system. I use this code to fetch the body of the email
$bodyText = imap_fetchbody($mbox,$v,1.2);
if(!strlen($bodyText)>0){
$bodyText = imap_fetchbody($mbox,$v,1);
}
This works perfectly but also includes the original message body, is there a way to only obtain the reply.
The whole email body looks like this
**This is a test
--
Kind Regards,
-----Original Message-----
From: -------------
To:--------------------------
Subject: You have received a reply to your enquiry
New System
Date: Tue, 5 Jul 2011 16:24:28 +0200
Good day,
You have received the following reply to an enquiry you made
-------------------------------------------------------------------------------------------------
test3
--------------------------------------------------------------------------------------------------
To reply to this comment please click on the Reply To button and add the message you wish to send.**
Is there a way to get only the body of the message without the ORIGINAL MESSAGE USING IMAP FUNCTIONS OR other PHP functions?
There is a blog post here: http://www.damnsemicolon.com/php/php-parse-email-body-email-piping that addresses the issue. It uses commonly-known email client reply formats to make a best-guess at what the reply body is. That's as good as you can get, given that you can't control the email clients being used.
Just a note, the linked code uses the from and to name/email to better parse the email address. You can get those values like so:
$headerInfo = imap_headerinfo($mbox,$v);
$fromEmail = $headerInfo->from->personal;
$fromName = $headerInfo->from->mailbox . "@" . $headerInfo->from->host;
$toEmail = $headerInfo->to->personal;
$toName = $headerInfo->to->mailbox . "@" . $headerInfo->to->host;
Edit: the link seems to be dead, here's the google cache: http://webcache.googleusercontent.com/search?q=cache:5MxzHXFU5TkJ:www.damnsemicolon.com/php/php-parse-email-body-email-piping+&cd=1&hl=en&ct=clnk&gl=ca
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With