2012-03-13 5 views
1

전자 메일의 머리글과 텍스트를 구분하는 PHP 스크립트가 있습니다. 필자는 이메일을 사용자의 입력 파일로 사용할 수 있도록 Perl 스크립트로 변환하려고했습니다.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); 

난 그냥 내가 그것을 펄 스크립트로 실행할 수 있도록해야 변경됩니다 무엇인지 알 필요가 : 다음은 PHP 스크립트입니다?

+2

당신이 펄에서 nativly를 실행하고 싶다면 perl로 다시 작성해야합니다! – Sgoettschkes

+3

perl과 php가 다른 언어이기 때문에 첫 번째 # 이후 거의 모든 것을 바꿔야합니다. – AD7six

+2

http://p3rl.org/Courriel은 MIME 메시지를 구문 분석합니다. – daxim

답변

10

이메일과 Perl을 다루는 거의 모든 것이 있다면 Email::Simple을 원할 것입니다. 나는 이것이 PHP 스크립트가하는 것 (PHP에서 더 좋은 방법이 있어야하지만)에 가깝다고 생각한다. 당신은 이메일 :: 간단한 객체를 생성하면, 당신은 단지 당신이 그들을 추출 방법에 대해 생각하지 않고 원하는 부분을 요청하십시오 MIME 부품

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 

는 또한 Email::MIME있다. 설명서의 예제를 통해 알아낼 수 있어야합니다. 이미지, 영화, 바이너리 PDF 및 터미널을 망칠 수있는 다른 것들을 고려해 볼 때 왜 왜 인쇄해야하는지 모르겠습니다.

행운을 빌어,

관련 문제