Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only a certain part of an email body using imap_body

Tags:

php

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?

like image 654
Elitmiar Avatar asked Nov 13 '22 20:11

Elitmiar


1 Answers

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

like image 199
Jonathan Amend Avatar answered Dec 20 '22 04:12

Jonathan Amend