2009-11-30 6 views
2

다음 이메일 스크립트를 사용하여 이메일을 처리하고 다양한 부분을 나중에 처리 할 변수에 넣습니다.그림 첨부 파일을 PHP로 이메일에서 분리하기

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

임 내가 사진 첨부 파일을 받아 내가 그러나 그것을 처리 할 수있는 난에 필요하므로 변수에 그를 분리하기 위해 시작하는 것입니다 경우 궁금해.

답변

0

당신이하고있는 것보다 더 많이 할 필요가 있습니다. 헤더에서 마임 경계를 감지 한 다음 멀티 파트 경계를 찾고 텍스트를 unbase64해야합니다. 이런 종류의 일에 library을 사용하는 것이 훨씬 낫습니다.

+0

난의 affraid 무엇 이잖아 말할 수 MimeMailParse (http://code.google.com/p/php-mime-mail-parser/) 를 사용합니다! 팁 btw 덕분에 – mrpatg

2

나는 그런 당신이 단순히 오프 Google에,

 
$parser = new MimeMailParser(); 
$parser->setStream(STDIN); 

// Handle images 
$path = '/tmp/'; 
$filename = ''; 
$attachments = $parser->getAttachments(); 
foreach ($attachments as $attachment) { 
    if (preg_match('/^image/', $attachment->content_type, $matches)) { 
     $pathinfo = pathinfo($attachment->filename); 
     $filename = $pathinfo['filename']; 

     if ($fp = fopen($path.$filename, 'w')) { 
      while ($bytes = $attachment->read()) { 
       fwrite($fp, $bytes); 
      } 
      fclose($fp); 
     } 
    } 
} 
+0

좋은 발견, 아픈보세요 더 – mrpatg

관련 문제