Exemple #1
0
 /**
  * Configures and fetches a singleton instance of `Zend_Mail`.
  * Also clears settings from previous use onthe `Zend_Mail` object
  * unless you call `getInstance(false)`.
  */
 public static function getInstance($clear = true)
 {
     // Has it been requested before?
     if (self::$mailer === null) {
         // Parse/verify configuration file
         self::$config = conf('Mailer');
         // Create the new Zend_Mail object
         self::$mailer = new Zend_Mail('UTF-8');
         // Set the transport method
         if (!isset(self::$config['transport']) || !isset(self::$config['transport']['type'])) {
             self::$error = 'No transport method defined.';
             return false;
         }
         switch (self::$config['transport']['type']) {
             case 'smtp':
                 $config = self::$config['transport'];
                 $host = $config['host'];
                 unset($config['host']);
                 unset($config['type']);
                 require_once 'Zend/Mail/Transport/Smtp.php';
                 self::$transport = new Zend_Mail_Transport_Smtp($host, $config);
                 break;
             case 'sendmail':
                 require_once 'Zend/Mail/Transport/Sendmail.php';
                 self::$transport = new Zend_Mail_Transport_Sendmail();
                 break;
             case 'file':
                 if (!isset(self::$config['transport']['path'])) {
                     self::$config['transport']['path'] = 'cache/mailer';
                 }
                 if (!file_exists(self::$config['transport']['path'])) {
                     mkdir(self::$config['transport']['path']);
                 }
                 require_once 'Zend/Mail/Transport/File.php';
                 self::$transport = new Zend_Mail_Transport_File(array('path' => self::$config['transport']['path'], 'callback' => function ($transport) {
                     // Files are named DATETIME-RECIPIENT-RAND.tmp
                     return $_SERVER['REQUEST_TIME'] . '-' . $transport->recipients . '-' . mt_rand() . '.tmp';
                 }));
                 break;
             default:
                 self::$error = 'Unknown transport type.';
                 return false;
         }
         Zend_Mail::setDefaultTransport(self::$transport);
         // Set default from info
         $email_from = self::$config['email_from'] !== 'default' ? self::$config['email_from'] : conf('General', 'email_from');
         $email_name = self::$config['email_name'] !== 'default' ? self::$config['email_name'] : conf('General', 'site_name');
         Zend_Mail::setDefaultFrom($email_from, $email_name);
     }
     // Clear mailer settings
     if ($clear) {
         self::$mailer->clearRecipients();
         self::$mailer->clearSubject();
         self::$mailer->clearFrom();
     }
     return self::$mailer;
 }
function mactrack_mail($to, $from, $fromname, $subject, $message, $headers = '')
{
    global $config;
    include_once $config['base_path'] . '/plugins/settings/include/mailer.php';
    $subject = trim($subject);
    $message = str_replace('<SUBJECT>', $subject, $message);
    $how = read_config_option('settings_how');
    if ($how < 0 && $how > 2) {
        $how = 0;
    }
    if ($how == 0) {
        $Mailer = new Mailer(array('Type' => 'PHP'));
    } else {
        if ($how == 1) {
            $sendmail = read_config_option('settings_sendmail_path');
            $Mailer = new Mailer(array('Type' => 'DirectInject', 'DirectInject_Path' => $sendmail));
        } else {
            if ($how == 2) {
                $smtp_host = read_config_option('settings_smtp_host');
                $smtp_port = read_config_option('settings_smtp_port');
                $smtp_username = read_config_option('settings_smtp_username');
                $smtp_password = read_config_option('settings_smtp_password');
                $Mailer = new Mailer(array('Type' => 'SMTP', 'SMTP_Host' => $smtp_host, 'SMTP_Port' => $smtp_port, 'SMTP_Username' => $smtp_username, 'SMTP_Password' => $smtp_password));
            }
        }
    }
    if ($from == '') {
        $from = read_config_option('mt_from_email');
        $fromname = read_config_option('mt_from_name');
        if ($from == '') {
            if (isset($_SERVER['HOSTNAME'])) {
                $from = 'Cacti@' . $_SERVER['HOSTNAME'];
            } else {
                $from = '*****@*****.**';
            }
        }
        if ($fromname == '') {
            $fromname = 'Cacti';
        }
        $from = $Mailer->email_format($fromname, $from);
        if ($Mailer->header_set('From', $from) === false) {
            cacti_log('ERROR: ' . $Mailer->error(), true, "MACTRACK");
            return $Mailer->error();
        }
    } else {
        $from = $Mailer->email_format($fromname, $from);
        if ($Mailer->header_set('From', $from) === false) {
            cacti_log('ERROR: ' . $Mailer->error(), true, "MACTRACK");
            return $Mailer->error();
        }
    }
    if ($to == '') {
        return 'Mailer Error: No <b>TO</b> address set!!<br>If using the <i>Test Mail</i> link, please set the <b>Alert e-mail</b> setting.';
    }
    $to = explode(',', $to);
    foreach ($to as $t) {
        if (trim($t) != '' && !$Mailer->header_set('To', $t)) {
            cacti_log('ERROR: ' . $Mailer->error(), true, "MACTRACK");
            return $Mailer->error();
        }
    }
    $wordwrap = read_config_option('settings_wordwrap');
    if ($wordwrap == '') {
        $wordwrap = 76;
    } else {
        if ($wordwrap > 9999) {
            $wordwrap = 9999;
        } else {
            if ($wordwrap < 0) {
                $wordwrap = 76;
            }
        }
    }
    $Mailer->Config['Mail']['WordWrap'] = $wordwrap;
    if (!$Mailer->header_set('Subject', $subject)) {
        cacti_log('ERROR: ' . $Mailer->error(), true, "MACTRACK");
        return $Mailer->error();
    }
    $text = array('text' => '', 'html' => '');
    $text['html'] = $message . '<br>';
    $text['text'] = strip_tags(str_replace('<br>', "\n", $message));
    $v = mactrack_version();
    $Mailer->header_set('X-Mailer', 'Cacti-MacTrack-v' . $v['version']);
    $Mailer->header_set('User-Agent', 'Cacti-MacTrack-v' . $v['version']);
    if ($Mailer->send($text) == false) {
        cacti_log('ERROR: ' . $Mailer->error(), true, "MACTRACK");
        return $Mailer->error();
    }
    return '';
}
Exemple #3
0
function send_mail($to, $from, $subject, $message, $filename = '', $headers = '')
{
    global $config;
    include_once $config["base_path"] . "/plugins/settings/include/mailer.php";
    $message = str_replace('<SUBJECT>', $subject, $message);
    $message = str_replace('<TO>', $to, $message);
    $message = str_replace('<FROM>', $from, $message);
    $how = read_config_option("settings_how");
    if ($how < 0 || $how > 2) {
        $how = 0;
    }
    if ($how == 0) {
        $Mailer = new Mailer(array('Type' => 'PHP'));
    } else {
        if ($how == 1) {
            $sendmail = read_config_option('settings_sendmail_path');
            $Mailer = new Mailer(array('Type' => 'DirectInject', 'DirectInject_Path' => $sendmail));
        } else {
            if ($how == 2) {
                $smtp_host = read_config_option("settings_smtp_host");
                $smtp_port = read_config_option("settings_smtp_port");
                $smtp_username = read_config_option("settings_smtp_username");
                $smtp_password = read_config_option("settings_smtp_password");
                $Mailer = new Mailer(array('Type' => 'SMTP', 'SMTP_Host' => $smtp_host, 'SMTP_Port' => $smtp_port, 'SMTP_Username' => $smtp_username, 'SMTP_Password' => $smtp_password));
            }
        }
    }
    if ($from == '') {
        $from = read_config_option('settings_from_email');
        $fromname = read_config_option('settings_from_name');
        if ($from == "") {
            if (isset($_SERVER['HOSTNAME'])) {
                $from = 'Cacti@' . $_SERVER['HOSTNAME'];
            } else {
                $from = '*****@*****.**';
            }
        }
        if ($fromname == "") {
            $fromname = "Cacti";
        }
        $from = $Mailer->email_format($fromname, $from);
        if ($Mailer->header_set('From', $from) === false) {
            print "ERROR: " . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    } else {
        $from = $Mailer->email_format('Cacti', $from);
        if ($Mailer->header_set('From', $from) === false) {
            print "ERROR: " . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    }
    if ($to == '') {
        return "Mailer Error: No <b>TO</b> address set!!<br>If using the <i>Test Mail</i> link, please set the <b>Alert e-mail</b> setting.";
    }
    $to = explode(',', $to);
    foreach ($to as $t) {
        if (trim($t) != '' && !$Mailer->header_set("To", $t)) {
            print "ERROR: " . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    }
    $wordwrap = read_config_option("settings_wordwrap");
    if ($wordwrap == '') {
        $wordwrap = 76;
    }
    if ($wordwrap > 9999) {
        $wordwrap = 9999;
    }
    if ($wordwrap < 0) {
        $wordwrap = 76;
    }
    $Mailer->Config["Mail"]["WordWrap"] = $wordwrap;
    if (!$Mailer->header_set("Subject", $subject)) {
        print "ERROR: " . $Mailer->error() . "\n";
        return $Mailer->error();
    }
    if (is_array($filename) && !empty($filename) && strstr($message, '<GRAPH>') !== 0) {
        foreach ($filename as $val) {
            $graph_data_array = array("output_flag" => RRDTOOL_OUTPUT_STDOUT);
            $data = @rrdtool_function_graph($val['local_graph_id'], $val['rra_id'], $graph_data_array);
            if ($data != "") {
                $cid = $Mailer->content_id();
                if ($Mailer->attach($data, $val['filename'] . '.png', "image/png", "inline", $cid) == false) {
                    print "ERROR: " . $Mailer->error() . "\n";
                    return $Mailer->error();
                }
                $message = str_replace('<GRAPH>', "<br><br><img src='cid:{$cid}'>", $message);
            } else {
                $message = str_replace('<GRAPH>', "<br><img src='" . $val['file'] . "'><br>Could not open!<br>" . $val['file'], $message);
            }
        }
    }
    $text = array('text' => '', 'html' => '');
    if ($filename == '') {
        $message = str_replace('<br>', "\n", $message);
        $message = str_replace('<BR>', "\n", $message);
        $message = str_replace('</BR>', "\n", $message);
        $text['text'] = strip_tags($message);
    } else {
        $text['html'] = $message . '<br>';
        $text['text'] = strip_tags(str_replace('<br>', "\n", $message));
    }
    if ($Mailer->send($text) == false) {
        print "ERROR: " . $Mailer->error() . "\n";
        return $Mailer->error();
    }
    return '';
}
         $html_part .= "&nbsp;&nbsp;&nbsp;&nbsp;<b>Type:</b> " . $data["type_description"] . "<br>\n";
         $html_part .= "&nbsp;&nbsp;&nbsp;&nbsp;<b>Committed Rate</b>: " . unit_display($data["threshold"]) . "<br>\n";
         $html_part .= "&nbsp;&nbsp;&nbsp;&nbsp;<b>Occured:</b> " . date($date_format, $data["end"]) . "<br>\n";
         $html_part .= "&nbsp;&nbsp;&nbsp;&nbsp;<b>Value:</b> " . unit_display($data["value"]) . "<br><br>\n";
         if ($data["type"] == "Nth") {
             $text_part .= "  *** If you continue at your current rate of usage, you will exceed your committed rate at time of billing. ***\n\n";
             $html_part .= "&nbsp;&nbsp;<b><i><font color=\"red\">If you continue at your current rate of usage, you will exceed your committed rate at time of billing.</font></i></b>\n\n";
         }
     }
 }
 /* Send email */
 $error = FALSE;
 if ($email["html"] == 1) {
     if ($obj_mail->send(array("text" => $text_part, "html" => $html_part)) === false) {
         print "ERROR: Unable to send mail to: " . $email["address"] . "\n";
         print "ERROR: " . $obj_mail->error() . "\n";
         $error = TRUE;
     }
 } else {
     if ($obj_mail->send($text_part) === false) {
         print "ERROR: Unable to send mail to: " . $email["address"] . "\n";
         print "ERROR: " . $obj_mail->error() . "\n";
         $error = TRUE;
     }
 }
 /* Kill mail object */
 $obj_mail->close();
 $obj_mail = FALSE;
 /* Update notification count in track file */
 if ($error == FALSE) {
     foreach ($threshold_exceed as $graph_id => $graph) {
Exemple #5
0
function thold_mail($to, $from, $subject, $message, $filename, $headers = '')
{
    global $config;
    thold_debug('Preparing to send email');
    include_once $config['base_path'] . '/plugins/settings/include/mailer.php';
    include_once $config['base_path'] . '/plugins/thold/setup.php';
    $subject = trim($subject);
    $message = str_replace('<SUBJECT>', $subject, $message);
    $how = read_config_option('settings_how');
    if ($how < 0 && $how > 2) {
        $how = 0;
    }
    if ($how == 0) {
        $Mailer = new Mailer(array('Type' => 'PHP'));
    } else {
        if ($how == 1) {
            $sendmail = read_config_option('settings_sendmail_path');
            $Mailer = new Mailer(array('Type' => 'DirectInject', 'DirectInject_Path' => $sendmail));
        } else {
            if ($how == 2) {
                $smtp_host = read_config_option('settings_smtp_host');
                $smtp_port = read_config_option('settings_smtp_port');
                $smtp_username = read_config_option('settings_smtp_username');
                $smtp_password = read_config_option('settings_smtp_password');
                $Mailer = new Mailer(array('Type' => 'SMTP', 'SMTP_Host' => $smtp_host, 'SMTP_Port' => $smtp_port, 'SMTP_Username' => $smtp_username, 'SMTP_Password' => $smtp_password));
            }
        }
    }
    if ($from == '') {
        $from = read_config_option('thold_from_email');
        $fromname = read_config_option('thold_from_name');
        if ($from == '') {
            if (isset($_SERVER['HOSTNAME'])) {
                $from = 'Cacti@' . $_SERVER['HOSTNAME'];
            } else {
                $from = 'Cacti@localhost';
            }
        }
        if ($fromname == '') {
            $fromname = 'Cacti';
        }
        $from = $Mailer->email_format($fromname, $from);
        if ($Mailer->header_set('From', $from) === false) {
            print 'ERROR: ' . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    } else {
        $from = $Mailer->email_format('Cacti', $from);
        if ($Mailer->header_set('From', $from) === false) {
            print 'ERROR: ' . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    }
    if ($to == '') {
        return 'Mailer Error: No <b>TO</b> address set!!<br>If using the <i>Test Mail</i> link, please set the <b>Alert e-mail</b> setting.';
    }
    $to = explode(',', $to);
    foreach ($to as $t) {
        if (trim($t) != '' && !$Mailer->header_set('To', $t)) {
            print 'ERROR: ' . $Mailer->error() . "\n";
            return $Mailer->error();
        }
    }
    $wordwrap = read_config_option('settings_wordwrap');
    if ($wordwrap == '') {
        $wordwrap = 76;
    }
    if ($wordwrap > 9999) {
        $wordwrap = 9999;
    }
    if ($wordwrap < 0) {
        $wordwrap = 76;
    }
    $Mailer->Config['Mail']['WordWrap'] = $wordwrap;
    if (!$Mailer->header_set('Subject', $subject)) {
        print 'ERROR: ' . $Mailer->error() . "\n";
        return $Mailer->error();
    }
    if (is_array($filename) && !empty($filename) && strstr($message, '<GRAPH>') !== 0) {
        foreach ($filename as $val) {
            $graph_data_array = array('output_flag' => RRDTOOL_OUTPUT_STDOUT);
            if (function_exists('imagecreatefrompng') && function_exists('imagejpeg')) {
                $data = @png2jpeg(rrdtool_function_graph($val['local_graph_id'], $val['rra_id'], $graph_data_array));
                $ext = 'jpg';
            } else {
                $data = @rrdtool_function_graph($val['local_graph_id'], $val['rra_id'], $graph_data_array);
                $ext = 'png';
            }
            if ($data != '') {
                $cid = $Mailer->content_id();
                if ($Mailer->attach($data, $val['filename'] . ".{$ext}", "image/{$ext}", 'inline', $cid) == false) {
                    print 'ERROR: ' . $Mailer->error() . "\n";
                    return $Mailer->error();
                }
                $message = str_replace('<GRAPH>', "<br><br><img src='cid:{$cid}'>", $message);
            } else {
                $message = str_replace('<GRAPH>', "<br><img src='" . $val['file'] . "'><br>Could not open!<br>" . $val['file'], $message);
            }
        }
    }
    $text = array('text' => '', 'html' => '');
    if ($filename == '') {
        $message = str_replace('<br>', "\n", $message);
        $message = str_replace('<BR>', "\n", $message);
        $message = str_replace('</BR>', "\n", $message);
        $text['text'] = strip_tags($message);
    } else {
        $text['html'] = $message . '<br>';
        $text['text'] = strip_tags(str_replace('<br>', "\n", $message));
    }
    $v = thold_version();
    $Mailer->header_set('X-Mailer', 'Cacti-Thold-v' . $v['version']);
    $Mailer->header_set('User-Agent', 'Cacti-Thold-v' . $v['version']);
    if (read_config_option('thold_email_prio') == 'on') {
        $Mailer->header_set('X-Priority', '1');
    }
    thold_debug("Sending email to '" . trim(implode(',', $to), ',') . "'");
    if ($Mailer->send($text) == false) {
        print 'ERROR: ' . $Mailer->error() . "\n";
        return $Mailer->error();
    }
    return '';
}