1
votes

How to fetch the text content of replied mail from gmail and outlook without original message and mail provided automated text

Like

OUTLOOK:

From: [email protected]

To: [email protected]

Subject: test reply

Date: Thu, 21 May 2015 05:34:42 +0000

GMAIL:

On 20 May 2015 at 17:52, New User wrote:

Also remove user email signature.

1
@Epodax I Posted what i tried - gvgvgvijayan

1 Answers

1
votes

So based on the above scenario I prepared an utility class which helped to solved the issue

<?php

/**
 * Thic Utility class is used to piping the mail content.
 * 
 * 
 */
class EmailPiping {

    public $gmailRegex = '#\R+^on.*wrote:(.*)#msi';
    public $outlookRegex = '#\R*From: (.*)#msi';

    /**
     * This method is used to filter only replied text from the gmail text 
     * content.
     * 
     * 
     * @param string $content
     * @return string return piped reply text only
     */
    public function pipeGMailReplyText($content) {
        return nl2br(preg_replace($this->gmailRegex, "", $content));
    }

    /**
     * This method is used to filter only replied text from the outlook mail 
     * text content.
     * 
     * 
     * @param string $content
     * @return string return piped reply text only
     */
    public function pipeOutlookReplyText($content) {
        return nl2br(preg_replace($this->outlookRegex, "", $content));
    }

    /**
     * This the generic method which is used when user don't know from which 
     * mail service provider the email was arrived means he just pass the 
     * content it will detect based on preg match and apply matched email's 
     * method call.
     * 
     * 
     * @param string $content
     * @return string return piped reply text only
     */
    public function pipeReplyText($content) {
        $fp = fopen($_SERVER["DOCUMENT_ROOT"] . "/logfile/EmailPiping.txt", "w");

        if (preg_match($this->gmailRegex, $content)) {
            $result = $this->pipeGMailReplyText($content);
        } else if (preg_match($this->outlookRegex, $content)) {
            $result = $this->pipeOutlookReplyText($content);
        } else {
            $result = nl2br($content);
        }

        fputs($fp, $result);
        return $result;
    }

}