예제 #1
0
function mail_customer($email, $text, $subject = '', $is_html = 0, $attachments = '', $add_unsubscribe = 0, $name = "", $is_guest = '0', $is_newsletter = '0', $bcc = "")
{
    global $config, $db;
    require_once $config['root_dir'] . "/includes/phpmailer/class.phpmailer.php";
    if (!strlen($email)) {
        return;
    }
    if (preg_match('/^Subject: (.+?)[\\n\\r]+/im', $text, $args)) {
        // found subject in body of message! then save it and remove from
        // message
        $subject = $args[1];
        $text = preg_replace('/(^Subject: .+?[\\n\\r]+)/im', '', $text);
    }
    $charset = "iso-8859-1";
    if (preg_match('/^Charset: (.+?)[\\n\\r]+/im', $text, $args)) {
        // found subject in body of message! then save it and remove from
        // message
        $charset = $args[1];
        $text = preg_replace('/(^Charset: .+?[\\n\\r]+)/im', '', $text);
    }
    if (preg_match('/^Format: (\\w+?)\\s*$/im', $text, $args)) {
        $format = $args[1];
        if (!strcasecmp('MULTIPART', $format)) {
            $is_html = 2;
            $text = preg_replace('/^Format: (\\w+?)\\s*$/im', '', $text);
        } elseif (!strcasecmp('HTML', $format)) {
            $is_html = 1;
            $text = preg_replace('/^Format: (\\w+?)\\s*$/im', '', $text);
        } elseif (!strcasecmp('TEXT', $format)) {
            $is_html = 0;
            $text = preg_replace('/^Format: (\\w+?)\\s*$/im', '', $text);
        }
    }
    if (preg_match_all('/^Attachment: (.+?)\\s*$/im', $text, $args)) {
        foreach ($args[1] as $fname) {
            $fname = str_replace('..', '', $fname);
            //            if ($fname[0] != '/')
            $fname = $config['root_dir'] . '/templates/' . $fname;
            if (!file_exists($fname)) {
                $db->log_error("Email attachment file : '{$fname}' is not exists - check your e-mail templates");
            } elseif (!is_readable($fname)) {
                $db->log_error("Email attachment file : '{$fname}' is not readable for the script - check your e-mail templates and/or chmod file");
            } else {
                $attachments[] = array('name' => basename($fname), 'type' => determine_mime_type(basename($fname)), 'tmp_name' => $fname);
            }
        }
        $text = preg_replace('/^Attachment: (.+?)\\s*$/im', '', $text);
    }
    if (preg_match('/"*(.*?)"*\\s*\\<(.+?)\\>\\s*$/', $email, $regs)) {
        $email_only = $regs[2];
        if ($name == '') {
            $name = $regs[1];
        }
    } else {
        $email_only = $email;
    }
    $mail =& new PHPMailer();
    $mail->CharSet = $charset;
    $mail->From = $config['admin_email_from'] ? $config['admin_email_from'] : $config['admin_email'];
    $mail->FromName = $config['admin_email_name'] ? $config['admin_email_name'] : $config['site_title'];
    if ($config['email_method'] == 'smtp') {
        if (preg_match('/^(.+?):(.+?)@(.+)$/', $config['smtp_host'], $regs)) {
            $mail->Username = $regs[1];
            $mail->Password = $regs[2];
            $mail->SMTPAuth = true;
            $mail->Host = $regs[3];
        } else {
            $mail->Host = $config['smtp_host'];
            if ($config['smtp_user'] && $config['smtp_pass']) {
                $mail->SMTPAuth = true;
                $mail->Username = $config['smtp_user'];
                $mail->Password = $config['smtp_pass'];
            }
        }
        if ($config['smtp_port']) {
            $mail->Port = $config['smtp_port'];
        }
        $mail->SMTPSecure = $config['smtp_security'];
        $mail->Mailer = "smtp";
    }
    if ($config['email_method'] == 'sendmail') {
        $mail->Mailer = "sendmail";
        $mail->Sendmail = $config['sendmail_path'];
    }
    if ($attachments) {
        foreach ((array) $attachments as $a) {
            $file_type = $a['type'];
            if (!strlen($file_type)) {
                $file_type = "application/octet-stream";
            }
            if ($file_type == 'application/x-msdownload') {
                $file_type = "application/octet-stream";
            }
            if ($a['tmp_name'] != '' && is_file($a['tmp_name'])) {
                $mail->AddAttachment($a['tmp_name'], $a['name'], "base64", $file_type);
            } else {
                $mail->AddStringAttachment($a['string'], $a['name'], "base64", $file_type);
            }
        }
    }
    if ($is_html) {
        $mail->isHTML(true);
    }
    if ($is_html == 2) {
        list($text, $plain_text) = preg_split('/^!{10}!+\\s*$/m', $text);
        if ($add_unsubscribe) {
            $plain_text = add_unsubscribe_link($email_only, $plain_text, 0, $is_guest, $is_newsletter);
        }
        $mail->AltBody = $plain_text;
    }
    if (!$config['disable_unsubscribe_link'] && $add_unsubscribe) {
        $text = add_unsubscribe_link($email_only, $text, $is_html, $is_guest, $is_newsletter);
    }
    $mail->Body = $text;
    $mail->Subject = $subject;
    $mail->AddAddress($email_only, $name);
    if ($bcc) {
        foreach ($bcc as $e) {
            if ($e) {
                $mail->AddBCC($e);
            }
        }
    }
    if (!$mail->Send()) {
        $db->log_error("There was an error sending the email message\n<br />" . $mail->ErrorInfo);
        $GLOBALS['AMEMBER_DONT_LOG_NEXT_ERROR'] = true;
        trigger_error("There was an error sending the email message", E_USER_WARNING);
    }
}
예제 #2
0
function display_preview()
{
    global $t, $config, $db, $vars;
    $users = get_target_users(0, 1, $total);
    $user = $users[0];
    $tt = $vars['text'];
    if (isset($user['is_guest']) && $user['is_guest'] == '1') {
        $is_guest = '1';
    } else {
        $is_guest = '0';
    }
    if (count($vars['newsletter_thread']) > 0) {
        $is_newsletter = '1';
    } else {
        $is_newsletter = '0';
    }
    $vars['text'] = add_unsubscribe_link($user['email'], $vars['text'], $vars['is_html'], $is_guest, $is_newsletter);
    $preview = array('text' => $vars['is_html'] ? get_email_message($vars, $user) : htmlspecialchars(get_email_message($vars, $user), ENT_NOQUOTES), 'subj' => htmlspecialchars(get_email_subject($vars, $user), ENT_NOQUOTES), 'to' => htmlspecialchars(get_email_to($vars, $user), ENT_NOQUOTES), 'is_html' => intval($vars['is_html']), 'files' => serialize($vars['files']));
    $vars['text'] = $tt;
    $t->assign('email_types', $email_types = get_email_types());
    $t->assign('vars', $vars);
    $t->assign('svars', serialize($vars));
    $t->assign('preview', $preview);
    $t->assign('total_members', $total);
    $t->assign('to_archive', 1);
    $t->assign('to_send', 1);
    $uploaded_files = array();
    foreach ((array) $vars['files'] as $f) {
        $uploaded_files[] = "{$f['name']} ({$f['size']} KB)";
    }
    if ($uploaded_files) {
        $t->assign('uploaded_files', $uploaded_files);
    }
    $t->display('admin/email_preview.html');
    $_SESSION['amember_send_mails'] = $vars;
}