From mail(): Lines should not belarger than 70 characters. Not 76 and not 72!
(PHP 5, PHP 7, PHP 8)
iconv_mime_encode — MIME
ヘッダフィールドを作成する
有効な MIME
ヘッダフィールドを作成し、返します。
これは以下のような形式になります。
Subject: =?ISO-8859-1?Q?Pr=FCfung_f=FCr?= Entwerfen von einer MIME kopfzeile
field_name
フィールド名。
field_value
フィールドの値。
options
iconv_mime_encode() の振る舞いを変更するには、 ここで設定項目を含む連想配列を指定します。 iconv_mime_encode() でサポートされている項目は 以下のとおりです。項目名の大文字・小文字は区別されることに注意してください。
項目 | 型 | 説明 | デフォルト値 | 例 |
---|---|---|---|---|
scheme | string |
フィールドの値のエンコード方法を指定します。"B" か "Q" の
どちらかを指定することになるでしょう。"B" は
base64 エンコードを、また "Q" は
quoted-printable エンコードを表します。
|
B | B |
input-charset | string |
第 1 パラメータ field_name と第 2 パラメータ
field_value の文字セットを指定します。
指定されなかった場合は、iconv_mime_encode()
は ini 設定
iconv.internal_encoding
であると仮定します。
|
iconv.internal_encoding | ISO-8859-1 |
output-charset | string |
MIME ヘッダを作成する文字セットを指定します。
|
iconv.internal_encoding | UTF-8 |
line-length | int | ヘッダ行の長さの最大値を指定します。もし結果がこの値より 長くなった場合は、 » RFC2822 - Internet Message Format に基づいてヘッダを "折りたたんで" 複数行に分割します。 指定されなかった場合は、長さは 76 文字に限定されます。 | 76 | 996 |
line-break-chars | string |
長いヘッダフィールドに対して "折りたたみ" 処理が行われる場合に
個々の行の後ろに付加される文字列を指定します。
指定されなかった場合は、"\r\n"
(CR LF )が用いられます。
このパラメータは、input-charset の値にかかわらず
常に ASCII 文字列として扱われることに注意してください。
|
\r\n | \n |
成功した場合はエンコードした MIME
フィールド、
エンコード時にエラーが発生した場合は false
を返します。
例1 iconv_mime_encode() の例:
<?php
$preferences = array(
"input-charset" => "ISO-8859-1",
"output-charset" => "UTF-8",
"line-length" => 76,
"line-break-chars" => "\n"
);
$preferences["scheme"] = "Q";
// この結果は "Subject: =?UTF-8?Q?Pr=C3=BCfung=20Pr=C3=BCfung?=" となります。
echo iconv_mime_encode("Subject", "Prüfung Prüfung", $preferences);
$preferences["scheme"] = "B";
// この結果は "Subject: =?UTF-8?B?UHLDvGZ1bmcgUHLDvGZ1bmc=?=" となります。
echo iconv_mime_encode("Subject", "Prüfung Prüfung", $preferences);
?>
From mail(): Lines should not belarger than 70 characters. Not 76 and not 72!
iconv_mime_encode() isn't directly suitable for encoding headers which include "specials" as described in RFC 1522 s4 & s5, for example
<?php
$mimeprefs = array ("scheme" => "Q",
"input-charset" => "utf-8",
"output-charset" => "utf-8",
"line-break-chars" => "\n");
$enc = iconv_mime_encode('From', '"Réal Namé" <user@example.com>', $prefs);
?>
will wrongly attempt to encode the angle brackets. To use the function in place of mb_encode_mimeheader(), instead you need to encode the words separately, removing the superfluous field name:
<?php
$encoded = "From: \"". preg_replace('/^:\s+/', '', iconv_mime_encode("", $real, $mimeprefs))."\" <$email>";
?>
Also, values of "line-length" greater than 76 would be illegal under RFC 1522 and resulting encoded words may not be recognised. (Not tested, but 72 would be safer.)
Looks like this function suffers from the same bug as mb_encode_mime() with long strings of non us-ascii characters. The function then returns false. This applies for utf-8 to utf-8 "conversion".
<?php
$subject = 'Вы находитесь здесь: Главная > продукт';
$prefs = array(
'scheme' => 'Q',
'input-charset' => 'UTF-8',
'output-charset' => 'UTF-8',
'line-length' => 76,
'line-break-chars' => "\r\n",
);
echo 'Original: ' . $subject . PHP_EOL;
$enc = iconv_mime_encode( 'Subject', $subject, $prefs );
var_dump( $enc ); // will show bool(false)
?>
As a workaround, you could explode() the value on spaces and encode each word separately. Then remove the "Subject: " in front of the resulting strings and join() them with "\r\n " (don't forget the SPACE after the \n) as separator.