php - 如何从管道获取电子邮件正文以进行编程

标签 php html email-headers

我正在通过管道将电子邮件发送到程序并运行一些代码。

**

我知道如何获取“发件人:”和“主题:”,但如何只获取电子邮件正文?

**

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

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

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

for ($i=0; $i < count($lines); $i++) 
{


    // look out for special headers
    if (preg_match("/Subject:/", $lines[$i], $matches)) 
        {

    list($One,$Subject) = explode("Subject:", $lines[$i]);    
    list($Subject,$Gone) = explode("<", $Subject);  


        }

等等...我如何获取电子邮件的正文内容?

最佳答案

基本上,您需要标题结束的位置,并了解它是否是多部分的,以便您可以获得电子邮件的正确部分。

这里是一些信息:

parsing raw email in php

第一个双换行符应该是电子邮件正文的开头。

这个页面可能会给你一些其他的想法(见下面的脚本):

http://thedrupalblog.com/configuring-server-parse-email-php-script

#!/usr/bin/php
<?php

// fetch data from stdin
$data = file_get_contents("php://stdin");

// extract the body
// NOTE: a properly formatted email's first empty line defines the separation between the headers and the message body
list($data, $body) = explode("\n\n", $data, 2);

// explode on new line
$data = explode("\n", $data);

// define a variable map of known headers
$patterns = array(
  'Return-Path',
  'X-Original-To',
  'Delivered-To',
  'Received',
  'To',
  'Message-Id',
  'Date',
  'From',
  'Subject',
);

// define a variable to hold parsed headers
$headers = array();

// loop through data
foreach ($data as $data_line) {

  // for each line, assume a match does not exist yet
  $pattern_match_exists = false;

  // check for lines that start with white space
  // NOTE: if a line starts with a white space, it signifies a continuation of the previous header
  if ((substr($data_line,0,1)==' ' || substr($data_line,0,1)=="\t") && $last_match) {

    // append to last header
    $headers[$last_match][] = $data_line;
    continue;

  }

  // loop through patterns
  foreach ($patterns as $key => $pattern) {

    // create preg regex
    $preg_pattern = '/^' . $pattern .': (.*)$/';

    // execute preg
    preg_match($preg_pattern, $data_line, $matches);

    // check if preg matches exist
    if (count($matches)) {

      $headers[$pattern][] = $matches[1];
      $pattern_match_exists = true;
      $last_match = $pattern;

    }

  }

  // check if a pattern did not match for this line
  if (!$pattern_match_exists) {
    $headers['UNMATCHED'][] = $data_line;
  }

}

?>

编辑

这是一个名为 MailParse 的 PHP 扩展:

http://pecl.php.net/package/mailparse

有人围绕它构建了一个名为 MimeMailParse 的类:

http://code.google.com/p/php-mime-mail-parser/

这是一篇讨论如何使用它的博客文章:

http://www.bucabay.com/web-development/a-php-mime-mail-parser-using-mailparse-extension/

关于php - 如何从管道获取电子邮件正文以进行编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724804/

相关文章:

php - 使用ajax和jquery发送数据 - textarea.live ('blur')

php - jQuery 使表单消失并依次出现

php - 在 PHP 中获取选定的选项值

jquery - 淡入/淡出 "Show More"按钮文本

email - Postfix,隔离多个站点的邮件 header ,这样如果一个站点被阻止/列入黑名单,共享服务器的其他站点也不会被列入黑名单

javascript - 如何模仿PHP? JavaScript 中的双问号行为?

javascript - 使 jQuery 放弃特定 div 类的操作

html - 悬停时更改列表元素内的文本和链接颜色

email - 强制创建新的电子邮件线程

php - 在 PHP 邮件函数中设置 $headers 数组时出现问题