简体   繁体   English

php电子邮件管道

[英]php email piping

Im trying to setup a program that will accept an incoming email and then break down the "sender" and "message" into php variables that i can then manipulate as needed, but im unsure where to start. 我试图设置一个程序,接受传入的电子邮件,然后将“发送者”和“消息”分解为php变量,然后我可以根据需要进行操作,但我不确定从哪里开始。

I already have the email address piped to the php file in question (via cpanel) 我已经将电子邮件地址传送到有问题的php文件(通过cpanel)

Start with: 从...开始:

$lines = explode("\n",$message_data);
$headers = array();
$body = '';

$in_body = false;

foreach($lines as $line)
{
     if($in_body)
     {
          $body .= $line;
     }
     elseif($line == '')
     {
          $in_body = true;
     }
     else
     {
          list($header_name,$header_value) = explode(':',$line,2);
          $headers[$header_name] = $header_body;
     }
}

// now $headers is an array of all headers and you could get the from address via $headers['From']
// $body contains just the body

I just wrote that off the top of my head; 我只是写下了我的头脑; haven't tested for syntax or errors. 没有测试语法或错误。 Just a starting point. 只是一个起点。

Have a look at the eZ Components ezcMailParser class. 看看eZ Components ezcMailParser类。 You'll need to implement an interface - ezcMailParserSet - to use it. 您需要实现一个接口 - ezcMailParserSet - 才能使用它。

Here is working solution 这是工作解决方案

#!/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;
}
}
echo $from;
echo $subject;
echo $headers;
echo $message;
?>

Works like a charm. 奇迹般有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM