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腳本?
如果你想讓它在Perl中運行,你必須在Perl中重新編寫它! – Sgoettschkes 2012-03-13 08:58:09
因爲perl和php是不同的語言,所以你需要在第一個#之後改變幾乎所有東西。 – AD7six 2012-03-13 08:58:55
http://p3rl.org/Courriel解析MIME消息。 – daxim 2012-03-13 09:24:56