/** * Encodes an email address header * * Unicode characters will be deaccented and encoded * quoted_printable for headers. * Addresses may not contain Non-ASCII data! * * Example: * mail_encode_address("föö <*****@*****.**>, me@somewhere.com","TBcc"); * * @param string $string Multiple adresses separated by commas * @param string $header Name of the header (To,Bcc,Cc,...) * @param boolean $names Allow named Recipients? */ function mail_encode_address($string, $header = '', $names = true) { $headers = ''; $parts = split(',', $string); foreach ($parts as $part) { $part = trim($part); // parse address if (preg_match('#(.*?)<(.*?)>#', $part, $matches)) { $text = trim($matches[1]); $addr = $matches[2]; } else { $addr = $part; } // skip empty ones if (empty($addr)) { continue; } // FIXME: is there a way to encode the localpart of a emailaddress? if (!utf8_isASCII($addr)) { msg(htmlspecialchars("E-Mail address <{$addr}> is not ASCII"), -1); continue; } if (!mail_isvalid($addr)) { msg(htmlspecialchars("E-Mail address <{$addr}> is not valid"), -1); continue; } // text was given if (!empty($text) && $names) { // add address quotes $addr = "<{$addr}>"; if (defined('MAILHEADER_ASCIIONLY')) { $text = utf8_deaccent($text); $text = utf8_strip($text); } if (!utf8_isASCII($text)) { $text = '=?UTF-8?Q?' . mail_quotedprintable_encode($text, 0) . '?='; } } else { $text = ''; } // add to header comma seperated and in new line to avoid too long headers if ($headers != '') { $headers .= ',' . MAILHEADER_EOL . ' '; } $headers .= $text . ' ' . $addr; } if (empty($headers)) { return null; } //if headername was given add it and close correctly if ($header) { $headers = $header . ': ' . $headers . MAILHEADER_EOL; } return $headers; }
/** * Encodes an email address header * * Unicode characters will be deaccented and encoded * quoted_printable for headers. * Addresses may not contain Non-ASCII data! * * Example: * mail_encode_address("föö <*****@*****.**>, me@somewhere.com","TBcc"); * * @param string $string Multiple adresses separated by commas * @param string $header Name of the header (To,Bcc,Cc,...) * @param boolean $names Allow named Recipients? */ function mail_encode_address($string, $header = '', $names = true) { $headers = ''; $parts = explode(',', $string); foreach ($parts as $part) { $part = trim($part); // parse address if (preg_match('#(.*?)<(.*?)>#', $part, $matches)) { $text = trim($matches[1]); $addr = $matches[2]; } else { $addr = $part; } // skip empty ones if (empty($addr)) { continue; } // FIXME: is there a way to encode the localpart of a emailaddress? if (!utf8_isASCII($addr)) { msg(htmlspecialchars("E-Mail address <{$addr}> is not ASCII"), -1); continue; } if (!mail_isvalid($addr)) { msg(htmlspecialchars("E-Mail address <{$addr}> is not valid"), -1); continue; } // text was given if (!empty($text) && $names) { // add address quotes $addr = "<{$addr}>"; if (defined('MAILHEADER_ASCIIONLY')) { $text = utf8_deaccent($text); $text = utf8_strip($text); } if (!utf8_isASCII($text)) { // put the quotes outside as in =?UTF-8?Q?"Elan Ruusam=C3=A4e"?= vs "=?UTF-8?Q?Elan Ruusam=C3=A4e?=" if (preg_match('/^"(.+)"$/', $text, $matches)) { $text = '"=?UTF-8?Q?' . mail_quotedprintable_encode($matches[1], 0) . '?="'; } else { $text = '=?UTF-8?Q?' . mail_quotedprintable_encode($text, 0) . '?='; } // additionally the space character should be encoded as =20 (or each // word QP encoded separately). // however this is needed only in mail headers, not globally in mail_quotedprintable_encode(). $text = str_replace(" ", "=20", $text); } } else { $text = ''; } // add to header comma seperated if ($headers != '') { $headers .= ','; if ($header) { $headers .= MAILHEADER_EOL . ' '; } // avoid overlong mail headers } $headers .= $text . ' ' . $addr; } if (empty($headers)) { return null; } //if headername was given add it and close correctly if ($header) { $headers = $header . ': ' . $headers . MAILHEADER_EOL; } return $headers; }
function test_russian_utf8() { $in = 'Ваш пароль для системы Доку Вики'; $out = '=D0=92=D0=B0=D1=88 =D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8C =D0=B4=D0=BB=D1=8F =D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D1=8B =D0=94=D0=BE=D0=BA=D1=83 =D0=92=D0=B8=D0=BA=D0=B8'; $this->assertEqual(mail_quotedprintable_encode($in, 0), $out); }