<?php
/* $KimmoSuominen: mbmail.php,v 1.3 2006/12/24 11:53:31 kim Exp $
 *
 * Copyright (c) 2006 Kimmo Suominen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in the documentation and/or other materials provided with the
 *    distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*
Plugin Name: Multibyte Mail
Plugin URI: http://kimmo.suominen.com/sw/mbmail/
Description: Encode 8-bit and multibyte characters in sent email messages.
Author: Kimmo Suominen
Version: 1.2
Author URI: http://kimmo.suominen.com/
*/

if (!function_exists('wp_mail')):

// The standard wp_mail() still exists when activating the plugin,
// so we avoid redegining in that case.

function wp_mail($to, $subject, $message, $headers = '')
{
    $charset = get_settings('blog_charset');
    if (empty($charset)) {
	$charset = 'UTF-8';
    }
    $blogname = get_settings('blogname');
    if (empty($blogname)) {
	$blogname = "WordPress";
    }
    $blogname = mb_encode_mimeheader($blogname, $charset, 'Q');
    mb_internal_encoding($charset);

    if ($headers == '') {
	$headers = "MIME-Version: 1.0\n"
	    . 'From: "' . $blogname . '" <wordpress@' . preg_replace(
		'#^www\.#',
		'',
		strtolower($_SERVER['SERVER_NAME'])
	    ) . ">\n"
	    . "Content-Type: text/plain; charset=\"" . $charset . "\"\n";
    }

    $to = mb_encode_mimeheader($to, $charset, 'Q');
    $subject = mb_encode_mimeheader($subject, $charset, 'Q');

    $lines = explode("\n", $headers);
    $i = '';
    foreach ($lines as $line) {
	if (empty($line)) {
	    continue;
	}
	if ($line[0] == ' ' || $line[0] == "\t") {
	    $harr[$i] .= ' ' . trim($line);
	} else {
	    list($i, $value) = explode(':', $line, 2);
	    $i = str_replace(' ', '-',
		ucwords(str_replace('-', ' ', strtolower($i)))
	    );
	    $harr[$i] = trim($value);
	}
    }
    foreach (array('From', 'Reply-To') as $h) {
	if (!empty($harr[$h])) {
	    list($name, $addr) = explode('<', $harr[$h]);
	    $name = trim(str_replace('"', '', $name));
	    $addr = trim(str_replace('>', '', $addr));
	    $harr[$h] = '"' . mb_encode_mimeheader($name, $charset, 'Q') . '"'
		. ' <' . $addr . '>';
	}
    }
    $headers = '';
    foreach ($harr as $hname => $hvalue) {
	$headers .= $hname . ': ' . $hvalue . "\n";
    }

    return @mail($to, $subject, $message, $headers);
}

endif;

?>