如何从管道获取电子邮件的正文以编程 [英] How to Get Body of email from Pipe to program

查看:160
本文介绍了如何从管道获取电子邮件的正文以编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



**



我知道如何获得发件人:和主题:,但我怎样才能得到电子邮件的正文?



**

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

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

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

($ i = 0; $ i {


//寻找特殊标题
if(preg_match(/ Subject:/,$ lines [$ i],$ matches))
{

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


}

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

解决方案

基本上,您想要标题结尾的位置,并知道它是否为多部分您可以获得电子邮件的正确部分。



以下是一些信息:

在php中解析原始邮件



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



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



http:// thedrupalblog。 com /configure-server-parse-email-php-script

 #!/ usr / bin / php 
<?php

//从stdin获取数据
$ data = file_get_contents(php:// stdin);

//提取主体
//注意:格式正确的电子邮件的第一个空行定义了标题和邮件正文之间的分隔
list($ data,$ body) = explode(\\\
\\\
,$ data,2);

//在新行上爆炸
$ data = explode(\\\
,$ data);

//定义已知标题的变量映射
$ patterns = array(
'Return-Path',
'X-Original-To',
'Delivered-To',
'Received',
'To',
'Message-Id',
'Date',
'From',
'主题',
);

//定义一个变量来保存解析的头文件
$ headers = array();

//通过数据循环
foreach($ data as $ data_line){

//对于每一行,假设一个匹配不存在
$ pattern_match_exists = false;

//检查以空格开始的行
//注意:如果一行以空格开始,则表示前一个标题
的延续如果(( substr($ data_line,0,1)==''|| substr($ data_line,0,1)==\t)&& $ last_match){

//附加到最后一个标题
$标头[$ last_match] [] = $ data_line;
继续;

}

//循环模式
foreach($ patterns as $ key => $ pattern){

//创建preg正则表达式
$ preg_pattern ='/ ^'。 $ pattern。':(。*)$ /';

//执行preg
preg_match($ preg_pattern,$ data_line,$ matches);

//检查preg匹配是否存在
if(count($ matches)){

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




//检查一个模式是否与该行不匹配
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/


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?

解决方案

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/

这篇关于如何从管道获取电子邮件的正文以编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆