如何将电子邮件正文从管道获取到程序

发布于 2024-11-02 03:28:48 字数 588 浏览 2 评论 0原文

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

**

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

**

#!/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);  


        }

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

I am piping an email to a program and running some code.

**

I know how to get the "From:" and the "Subject:" but how do I get only the body of the email?

**

#!/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);  


        }

etc... HOW DO I GET THE BODY CONTENT OF THE EMAIL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-11-09 03:28:48

基本上,您想要标头在哪里结束,并知道它是否是多部分的,以便您可以获得电子邮件的正确部分。

以下是一些信息:

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/

Basically, you want where the headers end, and to know if it's multipart or not so you can get the right portion(s) of the email.

Here is some information:

parsing raw email in php

Which says that the first double newline should be the beginning of the body of the email.

This page might give you some other ideas (see script below):

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

}

?>

EDIT

Here is a PHP extension called MailParse:

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

Somebody has built a class around it called MimeMailParse:

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

And here is a blog entry discussing how to use it:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文