2

私は最初のPHPコードに取り組んでおり、いくつかの電子メールの件名を表示したいと思います。

header('content-type: text/html; charset=utf-8');
...
$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$subject = utf8_encode($header->subject);

echo $subject;
echo "<br>";
...

しかし、件名「ääüüööß」の場合、出力は次のようになります。

=?ISO-8859-1?B?5OQg / Pwg9vYg3w ==?=

よろしく

解決:

Web( http://php.net/manual/de/function.imap-mime-header-decode.php )で非常に役立つ関数を見つけました。構文エラーが2つありますが、少し手直しした後は正常に機能しました。自分。

最終的に、ソリューションは次のようになります。

//return supported encodings in lowercase.
function mb_list_lowerencodings() { $r=mb_list_encodings();
  for ($n=sizeOf($r); $n--; ) { $r[$n]=strtolower($r[$n]); } return $r;
}

//  Receive a string with a mail header and returns it
// decoded to a specified charset.
// If the charset specified into a piece of text from header
// isn't supported by "mb", the "fallbackCharset" will be
// used to try to decode it.
function decodeMimeString($mimeStr, $inputCharset='utf-8',     
$targetCharset='utf-8',$fallbackCharset='iso-8859-1') {
$encodings=mb_list_lowerencodings();
$inputCharset=strtolower($inputCharset);
$targetCharset=strtolower($targetCharset);
$fallbackCharset=strtolower($fallbackCharset);

$decodedStr='';
$mimeStrs=imap_mime_header_decode($mimeStr);
for ($n=sizeOf($mimeStrs), $i=0; $i<$n; $i++) {
  $mimeStr=$mimeStrs[$i];
  $mimeStr->charset=strtolower($mimeStr->charset);
if (($mimeStr == 'default' && $inputCharset == $targetCharset)
  || $mimeStr->charset == $targetCharset) {
  $decodedStr.=$mimStr->text;
} else {
  $decodedStr.=mb_convert_encoding(
    $mimeStr->text, $targetCharset,
    (in_array($mimeStr->charset, $encodings) ?
      $mimeStr->charset : $fallbackCharset)

  );
}
} return $decodedStr;
}

...

$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$sub = decodeMimeString($header->subject);
echo $sub;
...

2つの関数は作成者@http://php.net/manual/de/function.imap-mime-header-decode.phpによって作成され、 2つの構文エラーを削除したことを指摘しておきます。

返信ありがとうございます

4

2 に答える 2

1

これは、「quotedprintable」と呼ばれるメールの一般的な形式です。非ASCII文字はすべてエンコードされます。(http://en.wikipedia.org/wiki/Quoted-printableを参照してください)

文字列はによってカプセル化されます

=?<encoding>?Q?<string>?=

<encoding>エンコーディングについて説明します。ここで:ISO8859-1

<string>文字列自体です

imap_mime_header_decode()を使用して文字列をデコードしてください(utf8_encode()を使用する前に)!

于 2012-07-21T18:52:51.337 に答える
0

ムハンマドの答えが十分でない場合は、文字列のエンコーディングを変更できるiconv関数を使用できます

iconv("ISO-8859-1", "UTF-8", $your_string);
于 2012-07-21T18:48:05.433 に答える