簡體   English   中英

PHP pipe 到腳本 email

[英]PHP pipe to script email

我正在嘗試將來自 support@example.com 的所有電子郵件轉發到 PHP 腳本以插入 MySql。

我的腳本有效,但我有兩個問題。

  1. from email 地址顯示發件人的名稱,而不是 email 地址。

  2. email 正文內容顯示例如。

    --00000000000095430105a9347fe3 內容類型:文本/純文本; 字符集="UTF-8"

這是一個測試 email

--00000000000095430105a9347fe3 內容類型:文本/html; 字符集="UTF-8"

這是一個測試 email

--00000000000095430105a9347fe3--

而不僅僅是 email 的主體這是一個測試 email

這是我的代碼:

$message = imap_fetchbody($mbox,1,'1');

$message = strip_tags(quoted_printable_decode(imap_fetchbody($mbox,1,"1")));
$message = strip_tags($message, '<body>');
$message = explode(">", $message);
$message = explode("<", $message[1]);
$message = str_replace("&nbsp;", "", $message[0]);
$message = html_entity_decode($message);
$message = trim($message);

我努力了

   $emailnumberPattern = "--[0]{12}";
   $replacement = " ";
   preg_replace($emailnumberPattern, $replacement, $string);
   $message .= $lines[$i]."\n";

我試過了,但不知道如何讓它工作:

 $emailnumberPattern = "--[0]{12}"; $replacement = " "; preg_replace($emailnumberPattern, $replacement, $string); $message.= $lines[$i]."\n";

歡迎任何幫助顯示發件人 email 以及從 email 正文中刪除不需要的內容。

解析電子郵件並非易事,只需幾個正則表達式即可解決。 您絕對應該使用專用的擴展程序或程序 此外,您應該避免將整個消息加載到單個字符串(或數組)中。

只是一個composer命令: composer require zbateson/mail-mime-parser

require __DIR__ . '/vendor/autoload.php';
$handle = fopen('php://stdin', 'r');
$message = \ZBateson\MailMimeParser\Message::from($handle);
fclose($handle);
$from = $message->getHeaderValue('from');
$body = $message->getTextContent();

也就是說,您可以使用您的代碼獲得一些結果,我已盡可能少地對其進行了修改:

 $lines = explode("\n", $email);

 // empty vars
 $from = "";
 $subject = "";
 $headers = "";
 $message = "";
 $splittingheaders = true;
 for ($i=0; $i < count($lines); $i++) {
 if ($splittingheaders) {
    // this is a header
    $headers .= $lines[$i]."\n";


    // look out for special headers
    if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
        $subject = $matches[1];
    }
    if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
        $from = $matches[1];
    }
    if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
        $to = $matches[1];
    }
    } else {
        if (preg_match('/Content-Type: text\/plain; charset=/', $lines[$i])) {
            $m = true;
            continue;
        }
        if (preg_match('/Content-Type:/', $lines[$i])) {
            $m = false;
        }
        if ($m) {
            $message .= $lines[$i]."\n";
        }
    }

    if (trim($lines[$i])=="") {
    // empty line, header section has ended
    $splittingheaders = false;
    }
 }
 if (preg_match('/ <(.*)>/', $from, $matches)){
     $from = $matches[1];
 }

這些結果無法保證,並且高度依賴於輸入格式。 要獲得強大的解決方案,請使用經過良好測試的解析器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM