簡體   English   中英

使用php腳本發送電子郵件管道來收集TO字段?

[英]Email piping with php script to collect the TO field?

我想將我的退回電子郵件轉發到php腳本來處理它們。 我在用。

 #!/usr/bin/php -q
 <?php

 // read from stdin
 $fd = fopen("php://stdin", "r");
 $email = "";
 while (!feof($fd)) {
$email .= fread($fd, 1024);
 }
   fclose($fd);

   // handle email
   $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];
    }
} else {
    // not a header, but message
    $message .= $lines[$i]."\n";
}

if (trim($lines[$i])=="") {
    // empty line, header section has ended
    $splittingheaders = false;
   }
   }

  ?>     

作品完美! 但是如何收集退回郵件中的“收件人”字段? 我試過只添加一個$變量,但它不起作用。

任何幫助都會很棒,

謝謝,

編輯:實際上我需要在消息正文中獲得“TO”字段。 - 它從中彈回的電子郵件。 如何拆分郵件正文以獲取特定信息? 我應該使用此人的電子郵件創建一個特殊標題,以便更容易獲得此信息嗎?

如果您可以創建自定義標頭,那將是最簡單的。 否則,您需要針對特定​​模式匹配您的整個身體; 如果您的正文可能會有所不同,則可能很難確保您始終匹配正確的文本。

自定義標題應以X-開頭,因此可能會執行以下操作:

if (preg_match("/^X-Originally-To: (.*)/", $lines[$i], $matches)) {
    $originallyto = $matches[1];
}

但是對於X-header,它們是非標准的,因此最好選擇一個名稱

  1. 通常僅用於相同目的,或
  2. 根本不可能被其他任何人使用

另外你應該注意的事情; 消息中的行應始終以“\\ r \\ n”結尾,因此您可能希望拆分兩個字符(而不僅僅是“\\ n”)以確保更一致的行為。

暫無
暫無

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

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