更改PHP脚本以作为Perl脚本运行

更改PHP脚本以作为Perl脚本运行,php,perl,Php,Perl,我有一个PHP脚本,用于分离电子邮件的标题和文本。我想把它转换成一个Perl脚本,这样它就可以将电子邮件作为用户的输入文件。 以下是PHP脚本: #!/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');

我有一个PHP脚本,用于分离电子邮件的标题和文本。我想把它转换成一个Perl脚本,这样它就可以将电子邮件作为用户的输入文件。 以下是PHP脚本:

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

对于几乎所有与电子邮件和Perl相关的内容,您可能需要。我认为这与PHP脚本的功能非常接近(尽管在PHP中必须有更好的方法)。创建Email::Simple对象后,只需请求所需的部分,而无需考虑如何提取它们:

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
使用电子邮件::简单;
我的$text=。。。;
我的$email=email::Simple->new($text);
我的($body)=$email->body;
我的($messageID、$replyToID、$subject、$to、$from)=
映射{scalar$email->header($124; | unde}qw(
消息id
答复
主题
到
从…起
);

如果您想让它在Perl中原生运行,那么必须在Perl中重新编写它!因为perl和php是不同的语言,所以在第一次之后几乎需要更改所有内容。解析MIME消息。