2012-10-05 2 views
0

첨부 파일을이 스크립트와 빠른 메일러 라이브러리를 사용하여 자동으로 다른 계정으로 전달하려고합니다. 문제는 어느 정도는 작동하지만 첨부 파일은 인코딩 된 텍스트로 전송됩니다. 첨부 파일을 그대로 보내고 싶었습니다. 나는 PHP에 익숙하지 않고 문제가 어디 있는지 알 수 없다. 도와주세요.첨부 파일을 인코딩 된 텍스트로 보냄

<?php 
require_once 'lib/swift_required.php'; 
$hostname = '{imap.asd.com:993/imap/ssl}INBOX'; 
$username = '[email protected]'; 
$password = 'ppwppw'; 

/* try to connect */ 
$connection = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error()); 

ini_set('memory_limit', '256M'); 

function Message_Parse($id) 

{ 

global $connection; 

    if (is_resource($connection)) 
    { 
     $result = array 
     (
      'text' => null, 
      'html' => null, 
      'attachments' => array(), 
     ); 

       $structure = imap_fetchstructure($connection, $id, FT_UID); 

     if (is_array($structure) && array_key_exists('parts', $structure)) 
     { 
      foreach ($structure->parts as $key => $part) 
      { 
       if (($part->type >= 2) || (($part->ifdisposition == 1) && ($part->disposition == 'ATTACHMENT'))) 
       { 
        $filename = null; 

        if ($part->ifparameters == 1) 
        { 
         $total_parameters = count($part->parameters); 

         for ($i = 0; $i < $total_parameters; $i++) 
         { 
          if (($part->parameters[$i]->attribute == 'NAME') || ($part->parameters[$i]->attribute == 'FILENAME')) 
          { 
           $filename = $part->parameters[$i]->value; 

           break; 
          } 
         } 

         if (is_null($filename)) 
         { 
          if ($part->ifdparameters == 1) 
          { 
           $total_dparameters = count($part->dparameters); 

           for ($i = 0; $i < $total_dparameters; $i++) 
           { 
            if (($part->dparameters[$i]->attribute == 'NAME') || ($part->dparameters[$i]->attribute == 'FILENAME')) 
            { 
             $filename = $part->dparameters[$i]->value; 

             break; 
            } 
           } 
          } 
         } 
        } 

        $result['attachments'][] = array 
        (
         'filename' => $filename, 
         'content' => str_replace(array("\r", "\n"), '', trim(imap_fetchbody($connection, $id, ($key + 1), FT_UID))), 
        ); 
       } 

       else 
       { 
        if ($part->subtype == 'PLAIN') 
        { 
         $result['text'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID); 
        } 

        else if ($part->subtype == 'HTML') 
        { 
         $result['html'] = imap_fetchbody($connection, $id, ($key + 1), FT_UID); 
        } 

        else 
        { 
         foreach ($part->parts as $alternative_key => $alternative_part) 
         { 
          if ($alternative_part->subtype == 'PLAIN') 
          { 
           echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>'; 

           $result['text'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID); 
          } 

          else if ($alternative_part->subtype == 'HTML') 
          { 
           echo '<h2>' . $alternative_part->subtype . ' ' . $alternative_part->encoding . '</h2>'; 

           $result['html'] = imap_fetchbody($connection, $id, ($key + 1) . '.' . ($alternative_key + 1), FT_UID); 
          } 
         } 
        } 
       } 
      } 
     } 

     else 
     { 
      $result['text'] = imap_body($connection, $id, FT_UID); 
     } 

     $result['text'] = imap_qprint($result['text']); 
     $result['html'] = imap_qprint(imap_8bit($result['html'])); 

     return $result; 

    } 

    return false; 
} 

$emails = imap_search($connection,'ALL'); 
    rsort($emails); 

foreach($emails as $email_number) { 

$result = Message_Parse($email_number); 
$data = $result['attachments']; 
$transport = Swift_MailTransport::newInstance(); 
$mailer = Swift_Mailer::newInstance($transport); 
$attachment = Swift_Attachment::newInstance($data, 'recorded.mp3', 'audio/mp3'); 
$message = Swift_Message::newInstance('new messaeg') 
    ->setFrom(array('[email protected]' => 'name')) 
    ->setTo(array('[email protected]')) 
    ->setBody($result['text'], 'Here is the message itself') 
    ->attach($attachment); 

    $result1 = $mailer->send($message); 

?> 
+0

아직도 나는 y 질문에 대한 적절한 답을 얻을 것이라고 생각한다. – ashajf

+0

그런 다음 더 많은 정보를 제공한다. 특히, 제기 된 질문에 답하십시오. – ficuscr

답변

0

$data = $result['attachments']; 이후 $ 데이터는 무엇입니까? 그것이 어떤 종류의 첨부 파일 컨테이너 객체라고 생각하십시오. 적절한 속성 (첨부 파일)을 얻어서 다시 첨부하기 위해 좀 더 자세히 조사해야 할 수도 있습니다. 참조 : http://dev.kayako.com/browse/SWIFT-2341

+0

또한 나에게 발생했습니다. 첨부 파일을 올바르게 풀지 못할 수도 있습니다. '인코딩 된 텍스트'에 대한 언급은 [RFC의] (http://tools.ietf.org/html/rfc2045)를 읽고 싶을 것이라고 생각합니다. 특히 mime 및 base64 인코딩과 관련됩니다. – ficuscr

0

이 잘 보이지 않는 :이 방법으로 첨부 파일을 만들 때

$data = $result['attachments']; 
... 
$attachment = Swift_Attachment::newInstance($data, 'recorded.mp3', 'audio/mp3'); 

, 나는 $data이 첨부 파일의 내용을 포함하는 문자열 있어야한다 생각합니다. 그러나 $result['attachments']은 원본 메시지의 모든 첨부 파일의 배열입니다. 이 첨부 파일을 반복해야하며 각 첨부 파일에 대해 별도의 Swift_Attachment를 만들어야합니다. 또한 파일 이름과 내용 유형을 원본 메시지에서 저장해야하며 녹음 된 .mp3 및 오디오/mp3로 하드 코딩하지 않아야합니다 (응용 프로그램이이 모든 것을 보장하지 않는 한).

MP3 인코딩은 일반 텍스트가 아니므로 무엇을 기대합니까?

+0

감사합니다. 그러나 나는 이것을 앞으로 가져 가는데 무력하다. – ashajf

관련 문제