2012-10-05 5 views
2

전자 메일 주소를 전달하고 잘못된 메시지 번호 오류가 발생하는 스크립트를 사용하고 있습니다. 메시지 번호는 함수에 대한 인수로 구문 분석되며 메시지 번호를 가져 오는 방법과 인수로 삽입하는 방법을 잘 모르겠습니다. 도와주세요. 나는 PHP에 처음이다. 오류는 $structure = imap_fetchstructure($connection, $id, FT_UID);$result['text'] = imap_body($connection, $id, FT_UID); 부분을 나타냅니다. 도와주세요.잘못된 메시지 번호 오류

<?php 
require_once '../swift/lib/swift_required.php'; 
$hostname = '{imap.xyz.com:993/imap/ssl}INBOX'; 
$username = 'email'; 
$password = 'password'; 

/* 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); 
     //print_r($structure); 
//array_key_exists — Checks if the given key or index exists in the array 
     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); 

    /* for every email... */ 
foreach($emails as $email_number) { 
    echo $email_number; 
$result = Message_Parse($email_number); 
//$data = $result['attachments']; 
$transport = Swift_MailTransport::newInstance(); 
$mailer = Swift_Mailer::newInstance($transport); 
//$attachment = Swift_Attachment::newInstance($result['attachments'], $filename, 'audio/mp3'); 
$message = Swift_Message::newInstance('test message1 ') 
    ->setFrom(array('from address' => 'name')) 
    ->setTo(array('to address.com')) 
    ->setBody($result['text']); 
// ->attach($attachment); 

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

?> 
+1

: 당신이 초보자이기 때문에

, 나는 거의 그래서 여기에 헤더 주어진 메시지의 내용을 얻을 수있는 방법 완벽한 예입니다,이 질문이라고 생각하지 왔어? – Rudu

+0

나는 메일을 보내기 위해 swiftmailer를 사용하고 있습니다. 일부 마법 코드가 들어가있다. – ashajf

답변

9

FT_UID 플래그는 오히려 메시지 시퀀스 ID를보다 메시지 UID를 사용하는 것을 의미한다. 주어진 메시지에 대한 uid를 얻으려면 msg_uid() 함수를 사용해야합니다. 이 마법`$의 mailer` 개체를 수행

<?php 

$emailAddress = '[email protected]'; 
$password = 'someSecretPassword'; 
$server = 'localhost'; 
$folder = 'Inbox'; 

$dsn = sprintf('{%s}%s', $server, $folder); 
$mbox = imap_open($dsn, $emailAddress, $password); 

if (!$mbox) { 
    die ('Unable to connect'); 
} 

$status = imap_status($mbox, $dsn, SA_ALL); 
$msgs = imap_sort($mbox, SORTDATE, 1, SE_UID); 

foreach ($msgs as $msguid) { 
    $msgno = imap_msgno($mbox, $msguid); 
    $headers = imap_headerinfo($mbox, $msgno); 
    $structure = imap_fetchstructure($mbox, $msguid, FT_UID); 

    var_dump($headers); 
    var_dump($structure); 
}