Exemplo n.º 1
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 '';
}
Exemplo n.º 2
0
/** generate_report		create the complete mail for a single report and send it
 * @param array $report	- complete row of reports table for the report to work upon
 * @param bool $force	- when forced, lastsent time will not be entered (e.g. Send Now)
 */
function generate_report($report, $force = false)
{
    global $config, $alignment;
    include_once $config["base_path"] . "/lib/time.php";
    include_once $config["base_path"] . "/lib/rrd.php";
    reports_log(__FUNCTION__ . ", report_id: " . $report["id"], false, "REPORTS TRACE", POLLER_VERBOSITY_MEDIUM);
    $body = reports_generate_html($report["id"], REPORTS_OUTPUT_EMAIL);
    $time = time();
    # get config option for first-day-of-the-week
    $first_weekdayid = read_graph_config_option("first_weekdayid");
    $offset = 0;
    $graphids = array();
    $attachments = array();
    while (true) {
        $pos = strpos($body, "<GRAPH:", $offset);
        if ($pos) {
            $offset = $pos + 7;
            $graph = substr($body, $pos + 7, 10);
            $arr = explode(":", $graph);
            $arr1 = explode(">", $arr[1]);
            $graphid = $arr[0];
            $timespan = $arr1[0];
            $graphids[$graphid . ":" . $timespan] = $graphid;
        } else {
            break;
        }
    }
    if (sizeof($graphids)) {
        foreach ($graphids as $key => $graphid) {
            $arr = explode(":", $key);
            $timesp = $arr[1];
            $timespan = array();
            # get start/end time-since-epoch for actual time (now()) and given current-session-timespan
            get_timespan($timespan, $time, $timesp, $first_weekdayid);
            # provide parameters for rrdtool graph
            $graph_data_array = array('graph_start' => $timespan["begin_now"], 'graph_end' => $timespan["end_now"], 'graph_width' => $report["graph_width"], 'graph_height' => $report["graph_height"], 'output_flag' => RRDTOOL_OUTPUT_STDOUT);
            if ($report["thumbnails"] == "on") {
                $graph_data_array['graph_nolegend'] = true;
            }
            switch ($report["attachment_type"]) {
                case REPORTS_TYPE_INLINE_PNG:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => 'graph_' . $graphid . ".png", 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_JPG:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".jpg", 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_GIF:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".gif", 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_ATTACH_PNG:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => 'graph_' . $graphid . ".png", 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_ATTACH_JPG:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".jpg", 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_ATTACH_GIF:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => 'graph_' . $graphid . ".gif", 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'attachment');
                    break;
                case REPORTS_TYPE_INLINE_PNG_LN:
                    $attachments[] = array('attachment' => @rrdtool_function_graph($graphid, "", $graph_data_array), 'filename' => '', 'mime_type' => 'image/png', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_JPG_LN:
                    $attachments[] = array('attachment' => png2jpeg(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => '', 'mime_type' => 'image/jpg', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_INLINE_GIF_LN:
                    $attachments[] = array('attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)), 'filename' => '', 'mime_type' => 'image/gif', 'graphid' => $graphid, 'timespan' => $timesp, 'inline' => 'inline');
                    break;
                case REPORTS_TYPE_ATTACH_PDF:
                    #				$attachments[] = array(
                    #					'attachment' => png2gif(@rrdtool_function_graph($graphid, "", $graph_data_array)),
                    #					'filename'   => 'graph_' . $graphid . ".gif",
                    #					'mime_type'  => 'image/gif',
                    #					'graphid'    => $graphid,
                    #					'timespan'   => $timesp,
                    #					'inline'     => 'attachment'
                    #				);
                    break;
            }
        }
    }
    if ($report["subject"] != '') {
        $subject = $report["subject"];
    } else {
        $subject = $report["name"];
    }
    if (!isset($report['bcc'])) {
        $report['bcc'] = '';
    }
    $v = db_fetch_cell("SELECT cacti FROM version");
    $headers['User-Agent'] = 'Cacti-Reports-v' . $v;
    $error = mailer(array($report['from_email'], $report['from_name']), $report['email'], '', $report['bcc'], '', $subject, $body, 'Cacti Reporting Requires and HTML Email Client', $attachments, $headers);
    session_start();
    if (strlen($error)) {
        if (isset($_REQUEST["id"])) {
            $_SESSION['reports_error'] = "Problems sending Report '" . $report['name'] . "'.  Problem with e-mail Subsystem Error is '{$error}'";
            if (!isset($_POST["selected_items"])) {
                raise_message("reports_error");
            }
        } else {
            reports_log(__FUNCTION__ . ", Problems sending Report '" . $report['name'] . "'.  Problem with e-mail Subsystem Error is '{$error}'", false, "REPORTS", POLLER_VERBOSITY_LOW);
        }
    } elseif (isset($_REQUEST)) {
        $_SESSION['reports_message'] = "Report '" . $report['name'] . "' Sent Successfully";
        if (!isset($_POST["selected_items"])) {
            raise_message("reports_message");
        }
    }
    if (!isset($_REQUEST["id"]) && !$force) {
        $int = read_config_option("poller_interval");
        if ($int == '') {
            $int = 300;
        }
        $next = reports_interval_start($report["intrvl"], $report["count"], $report["offset"], $report['mailtime']);
        $next = floor($next / $int) * $int;
        $sql = "UPDATE reports SET mailtime={$next}, lastsent=" . time() . " WHERE id = " . $report['id'];
        reports_log(__FUNCTION__ . ", update sql: " . $sql, false, "REPORTS TRACE", POLLER_VERBOSITY_MEDIUM);
        db_execute($sql);
    }
}