电子邮件管道 gmail 和 outlook 回复邮件

Email piping gmail and outlook replied mail

如何在没有原始邮件和邮件提供自动文本的情况下从gmail和outlook中获取回复邮件的文本内容

喜欢

展望:

发件人:test@gmail.com

收件人:测试@outlook.com

主题:测试回复

日期:2015 年 5 月 21 日,星期四 05:34:42 +0000

GMAIL:

2015 年 5 月 20 日在 17:52,新用户写道:

同时删除用户电子邮件签名。

所以根据上面的场景我准备了一个工具class帮助解决了这个问题

<?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;
    }

}