Esempio n. 1
0
/**
 * nv_sendmail()
 *
 * @param mixed $from
 * @param mixed $to
 * @param string $subject
 * @param string $message
 * @param string $files
 * @return
 */
function nv_sendmail($from, $to, $subject, $message, $files = '', $AddEmbeddedImage = false)
{
    global $global_config, $sys_info;
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer();
        $mail->SetLanguage(NV_LANG_INTERFACE);
        $mail->CharSet = $global_config['site_charset'];
        $mailer_mode = strtolower($global_config['mailer_mode']);
        if ($mailer_mode == 'smtp') {
            $mail->isSMTP();
            $mail->SMTPAuth = true;
            $mail->Port = $global_config['smtp_port'];
            $mail->Host = $global_config['smtp_host'];
            $mail->Username = $global_config['smtp_username'];
            $mail->Password = $global_config['smtp_password'];
            $SMTPSecure = intval($global_config['smtp_ssl']);
            switch ($SMTPSecure) {
                case 1:
                    $mail->SMTPSecure = 'ssl';
                    break;
                case 2:
                    $mail->SMTPSecure = 'tls';
                    break;
                default:
                    $mail->SMTPSecure = '';
            }
        } elseif ($mailer_mode == 'sendmail') {
            $mail->IsSendmail();
        } elseif (!in_array('mail', $sys_info['disable_functions'])) {
            $mail->IsMail();
        } else {
            return false;
        }
        $message = nv_url_rewrite($message);
        $message = nv_change_buffer($message);
        $message = nv_unhtmlspecialchars($message);
        $mail->From = $global_config['site_email'];
        $mail->FromName = $global_config['site_name'];
        if (is_array($from)) {
            $mail->addReplyTo($from[1], $from[0]);
        } else {
            $mail->addReplyTo($from);
        }
        if (empty($to)) {
            return false;
        }
        if (!is_array($to)) {
            $to = array($to);
        }
        foreach ($to as $_to) {
            $mail->addAddress($_to);
        }
        $mail->Subject = nv_unhtmlspecialchars($subject);
        $mail->WordWrap = 120;
        $mail->Body = $message;
        $mail->AltBody = strip_tags($message);
        $mail->IsHTML(true);
        if ($AddEmbeddedImage) {
            $mail->AddEmbeddedImage(NV_ROOTDIR . '/' . $global_config['site_logo'], 'sitelogo', basename(NV_ROOTDIR . '/' . $global_config['site_logo']));
        }
        if (!empty($files)) {
            $files = array_map('trim', explode(',', $files));
            foreach ($files as $file) {
                $mail->addAttachment($file);
            }
        }
        if (!$mail->Send()) {
            trigger_error($mail->ErrorInfo, E_USER_WARNING);
            return false;
        }
        return true;
    } catch (phpmailerException $e) {
        trigger_error($e->errorMessage(), E_USER_WARNING);
        return false;
    }
}