<?php
/*
Plugin Name: wp_mail Cyrillic
Version: 0.3
Plugin URI: http://mywordpress.ru/plugins/wp_mail_cyr
Description: Позволяет получать письма в кодировках, отличных от кодировки блога. Версия 0.3: <a href="http://sergeybiryukov.ru/">Sergey Biryukov</a>.
Author: Anton Skorobogatov (MyWordPress.ru)
Author URI: http://skorobogatov.ru/
*/
$utf8_cp1251_map = array(
"\xD0\x8E" => "\xA1", // ў
"\xD1\x9E" => "\xA2", // Ў
"\xD0\x81" => "\xA8", // Ё
"\x04\x04" => "\xAA", // Є
"\xD0\x87" => "\xAF", // Ї
"\x04\x06" => "\xB2", // ?
"\x04\x56" => "\xB3", // ?
"\xD1\x91" => "\xB8", // ё
"\x04\x54" => "\xBA", // є
"\x04\x57" => "\xBF", // ї
"\xD0\x90" => "\xC0", // А русское
"\xD0\x91" => "\xC1", // Б
"\xD0\x92" => "\xC2", // В
"\xD0\x93" => "\xC3", // и т.д.
"\xD0\x94" => "\xC4",
"\xD0\x95" => "\xC5",
"\xD0\x96" => "\xC6",
"\xD0\x97" => "\xC7",
"\xD0\x98" => "\xC8",
"\xD0\x99" => "\xC9",
"\xD0\x9A" => "\xCA",
"\xD0\x9B" => "\xCB",
"\xD0\x9C" => "\xCC",
"\xD0\x9D" => "\xCD",
"\xD0\x9E" => "\xCE",
"\xD0\x9F" => "\xCF",
"\xD0\xA0" => "\xD0",
"\xD0\xA1" => "\xD1",
"\xD0\xA2" => "\xD2",
"\xD0\xA3" => "\xD3",
"\xD0\xA4" => "\xD4",
"\xD0\xA5" => "\xD5",
"\xD0\xA6" => "\xD6",
"\xD0\xA7" => "\xD7",
"\xD0\xA8" => "\xD8",
"\xD0\xA9" => "\xD9",
"\xD0\xAA" => "\xDA",
"\xD0\xAB" => "\xDB",
"\xD0\xAC" => "\xDC",
"\xD0\xAD" => "\xDD",
"\xD0\xAE" => "\xDE",
"\xD0\xAF" => "\xDF",
"\xD0\xB0" => "\xE0",
"\xD0\xB1" => "\xE1",
"\xD0\xB2" => "\xE2",
"\xD0\xB3" => "\xE3",
"\xD0\xB4" => "\xE4",
"\xD0\xB5" => "\xE5",
"\xD0\xB6" => "\xE6",
"\xD0\xB7" => "\xE7",
"\xD0\xB8" => "\xE8",
"\xD0\xB9" => "\xE9",
"\xD0\xBA" => "\xEA",
"\xD0\xBB" => "\xEB",
"\xD0\xBC" => "\xEC",
"\xD0\xBD" => "\xED",
"\xD0\xBE" => "\xEE",
"\xD0\xBF" => "\xEF",
"\xD1\x80" => "\xF0",
"\xD1\x81" => "\xF1",
"\xD1\x82" => "\xF2",
"\xD1\x83" => "\xF3",
"\xD1\x84" => "\xF4",
"\xD1\x85" => "\xF5",
"\xD1\x86" => "\xF6",
"\xD1\x87" => "\xF7",
"\xD1\x88" => "\xF8",
"\xD1\x89" => "\xF9",
"\xD1\x8A" => "\xFA",
"\xD1\x8B" => "\xFB",
"\xD1\x8C" => "\xFC",
"\xD1\x8D" => "\xFD", // э
"\xD1\x8E" => "\xFE", // ю
"\xD1\x8F" => "\xFF", // я
"\xC2\xAB" => "\x27", // <
"\xC2\xBB" => "\x27", // >
"\xC2\xB0" => "\xB0", // >
"\xE2\x80\x9C" => "\x22", // "
"\xE2\x80\x9D" => "\x22", // "
"\xE2\x80\xA2" => "\x95", // •
"\xE2\x80\x93" => "\x96", // -
"\xE2\x80\x94" => "\x97", // -
"\xE2\x84\x96" => "\xB9" // №
);
function wp_mail_cyr_convert($from, $to, $subject)
{
if ($from == '' || $to == '') {
return $subject;
}
global $utf8_cp1251_map;
switch ($to) {
case 'windows-1251':
$toStandard = 'CP1251';
break;
case 'koi8-r':
$toStandard = 'KOI8-R';
break;
}
if (function_exists('iconv')) {
return iconv($from, $toStandard, $subject);
} elseif (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($subject, $to, $from);
} elseif ($to == 'windows-1251' && $from == 'UTF-8') {
return strtr($subject, $utf8_cp1251_map);
} elseif ($to == 'koi8-r' && $from == 'UTF-8') {
return convert_cyr_string(strtr($subject, $utf8_cp1251_map), 'w', 'k');
}
}
if ( !function_exists( 'wp_mail' ) && get_option('wp_mail_cyr_charset') != get_settings('blog_charset')) :
function wp_mail($to, $subject, $message, $headers = '') {
global $phpmailer;
$mailcharset = get_option('wp_mail_cyr_charset');
$blogcharset = get_settings('blog_charset');
if ( !is_object( $phpmailer ) ) {
require_once(ABSPATH . WPINC . '/class-phpmailer.php');
require_once(ABSPATH . WPINC . '/class-smtp.php');
$phpmailer = new PHPMailer();
}
$mail = compact('to', 'subject', 'message', 'headers');
$mail = apply_filters('wp_mail', $mail);
extract($mail, EXTR_SKIP);
if ( $headers == '' ) {
$headers = "MIME-Version: 1.0\n" .
"From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" .
"Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
}
$phpmailer->ClearAddresses();
$phpmailer->ClearCCs();
$phpmailer->ClearBCCs();
$phpmailer->ClearReplyTos();
$phpmailer->ClearAllRecipients();
$phpmailer->ClearCustomHeaders();
$phpmailer->FromName = "WordPress";
$phpmailer->AddAddress("$to", "");
$phpmailer->Subject = $subject;
$phpmailer->Body = $message;
$phpmailer->IsHTML(false);
$phpmailer->IsMail(); // set mailer to use php mail()
do_action_ref_array('phpmailer_init', array(&$phpmailer));
$mailheaders = (array) explode( "\n", $headers );
foreach ( $mailheaders as $line ) {
$header = explode( ":", $line );
switch ( trim( $header[0] ) ) {
case "From":
$from = trim( str_replace( '"', '', $header[1] ) );
if ( strpos( $from, '<' ) ) {
$phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
$from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
$from = str_replace( '>', '', $from );
} else {
$phpmailer->FromName = $from;
}
$phpmailer->From = trim( $from );
break;
default:
if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' )
$phpmailer->AddCustomHeader( $line );
break;
}
}
/**
* Добавление перекодировки при необходимости
*/
if ($blogcharset != $mailcharset && $mailcharset != '') {
$phpmailer->CharSet = $mailcharset;
$phpmailer->FromName = wp_mail_cyr_convert($blogcharset, $mailcharset, $phpmailer->FromName);
$phpmailer->Subject = wp_mail_cyr_convert($blogcharset, $mailcharset, $phpmailer->Subject);
$phpmailer->Body = wp_mail_cyr_convert($blogcharset, $mailcharset, $phpmailer->Body);
}
$result = @$phpmailer->Send();
return $result;
}
endif;
function wp_mail_cyr_options_page()
{
?>
<div class="wrap">
<h2>Настройки кодировок</h2>
<?php
if($_POST['wp_mail_cyr_charset']) {
// set the post formatting options
update_option('wp_mail_cyr_charset', $_POST['wp_mail_cyr_charset']);
echo '<div class="updated"><p>Настройки обновлены.</p></div>';
}
?>
<form method="post">
<fieldset class="options">
<legend>Отправлять письма в кодировке:</legend>
<?php
$charset = get_option('wp_mail_cyr_charset');
?>
<select name="wp_mail_cyr_charset">
<option value="UTF-8"<?php if($charset == 'UTF-8' || $charset==''){ echo(' selected="selected"');}?>>UTF-8</option>
<option value="windows-1251"<?php if($charset == 'windows-1251'){ echo(' selected="selected"');}?>>Windows-1251</option>
<option value="koi8-r"<?php if($charset == 'koi8-r'){ echo(' selected="selected"');}?>>KOI8-R</option>
</select>
<input type="submit" value="Изменить кодировку" />
</fieldset>
</form>
</div
<?
}
function wp_mail_cyr_add_menu()
{
add_options_page('Кодировки', 'Кодировки', 8, __FILE__, 'wp_mail_cyr_options_page');
}
add_action('admin_menu', 'wp_mail_cyr_add_menu');
?>