2012-03-13 102 views
1

我有用于分隔邮件头和文本的PHP脚本。我想将它转换为一个Perl脚本,以便它将电子邮件作为用户的输入文件。 以下是PHP脚本:对PHP脚本进行更改以作为Perl脚本运行

#!/usr/bin/php 
<?php 
//debug 
#ini_set ("display_errors", "1"); 
#error_reporting(E_ALL); 

//include email parser 
require_once('/path/to/class/rfc822_addresses.php'); 
require_once('/path/to/class/mime_parser.php'); 

// read email in from stdin 
$fd = fopen(ARGV[0], "r"); 
$email = ""; 
while (!feof($fd)) { 
    $email .= fread($fd, 1024); 
} 
fclose($fd); 

//create the email parser class 
$mime=new mime_parser_class; 
$mime->ignore_syntax_errors = 1; 
$parameters=array( 
    'Data'=>$email, 
); 

$mime->Decode($parameters, $decoded); 

//---------------------- GET EMAIL HEADER INFO -----------------------// 

//get the name and email of the sender 
$fromName = $decoded[0]['ExtractedAddresses']['from:'][0]['name']; 
$fromEmail = $decoded[0]['ExtractedAddresses']['from:'][0]['address']; 

//get the name and email of the recipient 
$toEmail = $decoded[0]['ExtractedAddresses']['to:'][0]['address']; 
$toName = $decoded[0]['ExtractedAddresses']['to:'][0]['name']; 

//get the subject 
$subject = $decoded[0]['Headers']['subject:']; 

$removeChars = array('<','>'); 

//get the message id 
$messageID = str_replace($removeChars,'',$decoded[0]['Headers']['message-id:']); 

//get the reply id 
$replyToID = str_replace($removeChars,'',$decoded[0]['Headers']['in-reply-to:']); 

//---------------------- FIND THE BODY -----------------------// 

//get the message body 
if(substr($decoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Body'])){ 

    $body = $decoded[0]['Body']; 

} elseif(substr($decoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Body'])) { 

    $body = $decoded[0]['Parts'][0]['Body']; 

} elseif(substr($decoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['Parts'][0]['Parts'][0]['Body'])) { 

    $body = $decoded[0]['Parts'][0]['Parts'][0]['Body']; 

} 

//print out our data 
echo " 

Message ID: $messageID 

Reply ID: $replyToID 

Subject: $subject 

To: $toName $toEmail 

From: $fromName $fromEmail 

Body: $body 

"; 

//show all the decoded email info 
print_r($decoded); 

我只需要知道什么变化我应该让它运行的Perl脚本?

+2

如果你想让它在Perl中运行,你必须在Perl中重新编写它! – Sgoettschkes 2012-03-13 08:58:09

+3

因为perl和php是不同的语言,所以你需要在第一个#之后改变几乎所有东西。 – AD7six 2012-03-13 08:58:55

+2

http://p3rl.org/Courriel解析MIME消息。 – daxim 2012-03-13 09:24:56

回答

10

几乎所有涉及电子邮件和Perl的东西,你可能都想要Email::Simple。我认为这很接近PHP脚本的功能(尽管PHP中必须有更好的方法)。一旦你创建了Email ::简单的对象,你刚才问你想不想着如何把它们提取出来的部分:

use Email::Simple; 

my $text = ...; 
my $email = Email::Simple->new($text); 

my($body) = $email->body; 
my($messageID, $replyToID, $subject, $to, $from) = 
     map { scalar $email->header($_) || undef } qw(
      message-id 
      reply-to 
      subject 
      to 
      from 
      ); 


print <<"HERE"; 
Message ID: $messageID 
Reply ID: $replyToID 
Subject: $subject 
To: $to 
From: $from 

Body: $body 
HERE 

对于MIME部分,这里还有Email::MIME。您应该能够从文档中的示例中找出它。我不知道你为什么要打印它,因为它可能是图像,电影,二进制PDF和其他可能会让你的终端搞砸的东西。

祝你好运:) :)