2013-03-21 45 views
11

Tôi có email được gửi qua mã hóa base64 và mã hóa 8 bit. Tôi đã tự hỏi làm thế nào tôi có thể kiểm tra mã hóa của tin nhắn bằng cách sử dụng imap_fetchstructure (đã làm điều này trong khoảng hai giờ, vì vậy bị mất) và sau đó giải mã nó đúng cách.Thông báo giải mã PHP IMAP

Gmail và hộp thư (ứng dụng trên iOS) đang gửi nó dưới dạng 8 bit trong khi ứng dụng Thư của Windows 8 gửi nó dưới dạng base64. Dù bằng cách nào, tôi cần phải giải mã cho dù 8bit hoặc base64 của nó bằng cách phát hiện loại mã hóa nó đã sử dụng.

Sử dụng PHP 5.1.6 (có, tôi nên cập nhật, bận).

Tôi thực sự không có mã để hiển thị. Đây là tất cả những gì tôi có:

<?php 
$hostname = '{********:993/imap/ssl}INBOX'; 
$username = '*********'; 
$password = '******'; 

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error()); 

$emails = imap_search($inbox,'ALL'); 

if($emails) { 

    $output = ''; 

    rsort($emails); 

    foreach($emails as $email_number) { 

     $overview = imap_fetch_overview($inbox,$email_number,0); 
     $message = imap_fetchbody($inbox,$email_number,2); 
     $struct = imap_fetchstructure($inbox, $email_number); 

     $output.= '<div class="toggle'.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
     $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
     $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
     $output.= '</div>'; 

     /* output the email body */ 
     $output.= '<div class="body">'.$message.'</div>'; 
    } 

    echo $output; 
} 

imap_close($inbox); 
?> 
+0

là những nhân vật không đến đúng cho bạn về những email? –

+0

@DeadMan Email từ Windows Mail (ứng dụng Metro trong Win8) là base64, trong khi các email đến từ các ứng dụng khác là 8 bit. – alexpja

+0

Kết quả của ['imap_fetchstructure()'] (http://php.net/manual/en/function.imap-fetchstructure.php) phải có thuộc tính 'encoding'. Điều đó không làm việc cho bạn? – Phil

Trả lời

33

imap_bodystruct() hoặc imap_fetchstructure() phải trả lại thông tin này cho bạn. Các mã sau đây nên làm chính xác những gì bạn đang tìm kiếm:

<?php 
$hostname = '{********:993/imap/ssl}INBOX'; 
$username = '*********'; 
$password = '******'; 

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to server: ' . imap_last_error()); 

$emails = imap_search($inbox,'ALL'); 

if($emails) { 
    $output = ''; 
    rsort($emails); 

    foreach($emails as $email_number) { 
     $overview = imap_fetch_overview($inbox,$email_number,0); 
     $structure = imap_fetchstructure($inbox, $email_number); 

     if(isset($structure->parts) && is_array($structure->parts) && isset($structure->parts[1])) { 
      $part = $structure->parts[1]; 
      $message = imap_fetchbody($inbox,$email_number,2); 

      if($part->encoding == 3) { 
       $message = imap_base64($message); 
      } else if($part->encoding == 1) { 
       $message = imap_8bit($message); 
      } else { 
       $message = imap_qprint($message); 
      } 
     } 

     $output.= '<div class="toggle'.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<span class="from">From: '.utf8_decode(imap_utf8($overview[0]->from)).'</span>'; 
     $output.= '<span class="date">on '.utf8_decode(imap_utf8($overview[0]->date)).'</span>'; 
     $output.= '<br /><span class="subject">Subject('.$part->encoding.'): '.utf8_decode(imap_utf8($overview[0]->subject)).'</span> '; 
     $output.= '</div>'; 

     $output.= '<div class="body">'.$message.'</div><hr />'; 
    } 

    echo $output; 
} 

imap_close($inbox); 
?> 
+0

Nó đã giải mã hầu hết các tin nhắn, ngoại trừ các tin nhắn từ Windows Mail trên Windows 8. Hy vọng rằng không ai sử dụng nó. Cảm ơn bạn! – alexpja

+0

Bạn có thể vui lòng kiểm tra [this] (http://stackoverflow.com/questions/39745784/how-to-fetch-to-and-cc-email-using-imap-php) cần được giúp đỡ –

+0

Tại sao shunt 4 có thể 6 mã hóa vào xử lý khối khác của quoted_printables? http://php.net/manual/en/function.imap-fetchstructure.php – Andrew

1

Đối tượng trả lại cho imap_fetchstructure()

  1. mã hóa (mã hóa chuyển Body)

mã hóa Chuyển giao (có thể thay đổi với sử dụng thư viện)

0 7BIT 1 8BIT 2 BINARY.210 3 Base64 4 quoted-in 5 KHÁC

$s = imap_fetchstructure($mbox,$mid); 
if ($s->encoding==3) 
    $data = base64_decode($data); 
5

Bạn có thể nhìn vào ví dụ này.

Imap/Imap

Dưới đây là đoạn mã

switch ($encoding) { 
    # 7BIT 
    case 0: 
     return $text; 
    # 8BIT 
    case 1: 
     return quoted_printable_decode(imap_8bit($text)); 
    # BINARY 
    case 2: 
     return imap_binary($text); 
    # BASE64 
    case 3: 
     return imap_base64($text); 
    # QUOTED-PRINTABLE 
    case 4: 
     return quoted_printable_decode($text); 
    # OTHER 
    case 5: 
     return $text; 
    # UNKNOWN 
    default: 
     return $text; 
} 
+0

Tôi đã phải sử dụng 'imap_qprint' trên các thông điệp ENC7BIT để hiển thị chính xác. Bạn có thể sử dụng [hằng số PHP] (http://php.net/manual/en/function.imap-fetchstructure.php#refsect1-function.imap-fetchstructure-returnvalues) thay vì số: 'case ENC7BIT:' – Genjo

Các vấn đề liên quan