从PHP Email管道传递参数 [英] passing paremeters from PHP Email piping

查看:72
本文介绍了从PHP Email管道传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,可以从管道式电子邮件转发器接收信息.

I have a PHP Script that received information from a piped email forwarder.

当前,一切正常.我只想操纵接收到的参数,以仅接收我想要的来自电子邮件的数据

Currently everything works perfectly. I just want to manipulate the received parameters to receive only the data I want from the email

脚本是:

#!/usr/local/bin/php -q

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


//get variables from email start


//split the string into array of strings, each of the string represents a single line, recieved
$lines = explode("\n", $email_content);

// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;

//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";

// Split out the subject portion
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split out the sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}

当前,变量$ email返回:

Currently, the variable $email returns:

"Person name" <person@domain.net> 

如何修改此变量以仅获取电子邮件部分?

How can I modify this variable to only get the email portion?

第二,变量消息返回一堆数据,包括格式和标签,如下所示:

Secondly, the variable message returns a whole pile of data including formatting and tags as below:

This is a multipart message in MIME format.

------=_NextPart_000_0031_01CD9CA8.9BE5F470
Content-Type: text/plain;
    charset="us-ascii"
Content-Transfer-Encoding: 7bit

testing


------=_NextPart_000_0031_01CD9CA8.9BE5F470
Content-Type: text/html;
    charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" = xmlns:o=3D"urn:schemas-microsoft-com:office:office" = xmlns:w=3D"urn:schemas-microsoft-com:office:word" = xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" = xmlns=3D"http://www.w3.org/TR/REC-html40"><head><META = HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; = charset=3Dus-ascii"><meta name=3DGenerator content=3D"Microsoft Word 14 = (filtered medium)"><style><!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:"Calibri","sans-serif";}
@page WordSection1
    {size:612.0pt 792.0pt;
    margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" /> </xml><![endif]--><!--[if gte mso 9]><xml> <o:shapelayout v:ext=3D"edit"> <o:idmap v:ext=3D"edit" data=3D"1" /> </o:shapelayout></xml><![endif]--></head><body lang=3DEN-US link=3Dblue = vlink=3Dpurple><div class=3DWordSection1><p class=3DMsoNormal>Ninety = nine<o:p></o:p></p></div></body></html>
------=_NextPart_000_0031_01CD9CA8.9BE5F470--

.我只想提取纯文本形式的身体内容.因此,如果电子邮件正文只是在测试",那么我希望变量返回测试?

. I want to extract only the content of my body in plain text. so if body of email was simply 'testing' then I want the variable to return testing?

帮助一如既往.

再次感谢.

按照@hakra编辑

#!/usr/local/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; 
} 
} 



$buffer = [$email] // Mail Content from Pipe 

$mail = mailparse_msg_create(); 
mailparse_msg_parse($mail, $buffer); 
$struct = mailparse_msg_get_structure($mail);  

foreach ($struct as $st) {  
    $section = mailparse_msg_get_part($mail, $st);  
    $info    = mailparse_msg_get_part_data($section);  
 }

 ?>

推荐答案

您可以使用

You can parse address lines with imap_rfc822_parse_adrlistDocs or mailparse_rfc822_parse_addressesDocs.

要解析电子邮件的正文,可以使用 邮件解析扩展名 文档 :

To parse the body of the email message, you can make use of the Mail Parse extensionDocs:

$buffer = [...] // Mail Content from Pipe

$mail = mailparse_msg_create();
mailparse_msg_parse($mail, $buffer);
$struct = mailparse_msg_get_structure($mail); 

foreach ($struct as $st) { 
    $section = mailparse_msg_get_part($mail, $st); 
    $info    = mailparse_msg_get_part_data($section); 

    print_r($info);
}

这篇关于从PHP Email管道传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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