Esempio n. 1
0
/**
* Takes a name and an email address and returns a string that vaguely
* resembles an email address specification conforming to RFC(2)822 ...
*
* @param    string  $name       name, e.g. John Doe
* @param    string  $address    email address only, e.g. john.doe@example.com
* @return   string              formatted email address
*
*/
function COM_formatEmailAddress($name, $address)
{
    $name = trim($name);
    $address = trim($address);
    if (function_exists('CUSTOM_formatEmailAddress')) {
        return CUSTOM_formatEmailAddress($name, $address);
    }
    $formatted_name = COM_emailEscape($name);
    // if the name comes back unchanged, it's not UTF-8, so preg_match is fine
    if ($formatted_name == $name && preg_match('/[^0-9a-z ]/i', $name)) {
        $formatted_name = str_replace('"', '\\"', $formatted_name);
        $formatted_name = '"' . $formatted_name . '"';
    }
    return $formatted_name . ' <' . $address . '>';
}
/**
* Custom email function for creating an email message in ISO-2022-JP
*/
function CUSTOM_mail($to, $subject, $message, $from = '', $html = false, $priority = 0, $cc = '')
{
    global $_CONF, $LANG_CHARSET;
    static $mailobj;
    include_once 'Mail.php';
    include_once 'Mail/RFC822.php';
    if (defined('CUSTOM_MAIL_DEBUG')) {
        COM_errorLog('CUSTOM_mail: to=' . $to . ' subject=' . $subject);
    }
    // 余分なヘッダを追加されないように改行コードを削除
    $to = substr($to, 0, strcspn($to, "\r\n"));
    $cc = substr($cc, 0, strcspn($cc, "\r\n"));
    $from = substr($from, 0, strcspn($from, "\r\n"));
    $subject = substr($subject, 0, strcspn($subject, "\r\n"));
    // Fromが空の場合は、サイト管理者のアドレスにする
    if (empty($from)) {
        $from = COM_formatEmailAddress($_CONF['site_name'], $_CONF['site_mail']);
    }
    // ヘッダをエスケープ(1.5.2では、この時点でエスケープ済み)
    // NOTE: version_compare(VERSION, '1.5.2')とすると、security releaseでは
    //       判定に失敗する
    preg_match("/^(\\d+\\.\\d+\\.\\d+).*\$/", VERSION, $match);
    if (version_compare($match[1], '1.5.2') < 0) {
        list($temp_to_comment, $temp_to_address) = CUSTOM_splitAddress($to);
        $to = CUSTOM_formatEmailAddress($temp_to_comment, $temp_to_address);
        list($temp_cc_comment, $temp_cc_address) = CUSTOM_splitAddress($cc);
        $cc = CUSTOM_formatEmailAddress($temp_cc_comment, $temp_cc_address);
        list($temp_from_comment, $temp_from_address) = CUSTOM_splitAddress($from);
        $from = CUSTOM_formatEmailAddress($temp_from_comment, $temp_from_address);
        $subject = CUSTOM_emailEscape($subject);
    }
    // 本文をエスケープ
    $message = CUSTOM_convertEncoding($message, CUSTOM_MAIL_ENCODING);
    $message = str_replace(array("\r\n", "\n", "\r"), CUSTOM_MAIL_BODY_LINEBREAK, $message);
    // メールオブジェクトを作成
    $method = $_CONF['mail_settings']['backend'];
    if (!isset($mailobj)) {
        if ($method === 'sendmail' or $method === 'smtp') {
            $mailobj =& Mail::factory($method, $_CONF['mail_settings']);
        } else {
            $mailobj =& Mail::factory($method);
        }
    }
    // ヘッダ組み立て
    $headers = array();
    $headers['From'] = $from;
    if ($method != 'mail') {
        $headers['To'] = $to;
    }
    if (!empty($cc)) {
        $headers['Cc'] = $cc;
    }
    $headers['Date'] = date('r');
    // RFC822 formatted date
    if ($method === 'smtp') {
        list($usec, $sec) = explode(' ', microtime());
        $m = substr($usec, 2, 5);
        $headers['Message-Id'] = '<' . date('YmdHis') . '.' . $m . '@' . $_CONF['mail_settings']['host'] . '>';
    }
    if ($html) {
        $headers['Content-Type'] = 'text/html; charset=' . CUSTOM_MAIL_ENCODING;
        $headers['Content-Transfer-Encoding'] = '8bit';
    } else {
        $headers['Content-Type'] = 'text/plain; charset=' . CUSTOM_MAIL_ENCODING;
    }
    $headers['Subject'] = $subject;
    if ($priority > 0) {
        $headers['X-Priority'] = $priority;
    }
    $headers['X-Mailer'] = 'Geeklog-' . VERSION . ' (' . CUSTOM_MAIL_ENCODING . ')';
    $retval = $mailobj->send($to, $headers, $message);
    if ($retval !== true) {
        COM_errorLog($retval->toString(), 1);
    }
    return $retval === true;
}