/** mailer - function to send mails to users * @arg $from - a string email address, or an array in array(email_address, name format) * @arg $to - either a string of comma delimited email addresses, or an array of addresses in email_address => name format * @arg $cc - either a string of comma delimited email addresses, or an array of addresses in email_address => name format * @arg $bcc - either a string of comma delimited email addresses, or an array of addresses in email_address => name format * @arg $replyto - a string email address, or an array in array(email_address, name format) * @arg $subject - the email subject * @arg $body - the email body, in HTML format. If content_text is not set, the function will attempt to extract * from the HTML format. * @arg $body_text - the email body in TEXT format. If set, it will override the stripping tags method * @arg $attachments - the emails attachments as an array * @arg $headers - an array of name value pairs representing custom headers. * @arg $html - if set to true, html is the default, otherwise text format will be used */ function mailer($from, $to, $cc, $bcc, $replyto, $subject, $body, $body_text = '', $attachments, $headers, $html = true) { global $config; include_once $config['base_path'] . '/lib/PHPMailer/PHPMailerAutoload.php'; // Set the to informaiotn 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.'; } if (is_array($to)) { $toText = $to[1] . ' <' . $to[0] . '>'; } else { $toText = $to; } if (is_array($from)) { $fromText = $from[1] . ' <' . $from[0] . '>'; } else { $fromText = $from; } $body = str_replace('<SUBJECT>', $subject, $body); $body = str_replace('<TO>', $toText, $body); $body = str_replace('<FROM>', $fromText, $body); // Create the PHPMailer instance $mail = new PHPMailer(); // Set a reasonable timeout of 5 seconds $timeout = read_config_option('settings_smtp_timeout'); if (empty($timeout) || $timeout < 0 || $timeout > 300) { $mail->Timeout = 5; } else { $mail->Timeout = $timeout; } // Set the subject $mail->Subject = $subject; $how = read_config_option('settings_how'); if ($how < 0 || $how > 2) { $how = 0; } if ($how == 0) { $mail->isMail(); } else { if ($how == 1) { $mail->Sendmail = read_config_option('settings_sendmail_path'); $mail->isSendmail(); } else { if ($how == 2) { $mail->isSMTP(); $mail->Host = read_config_option('settings_smtp_host'); $mail->Port = read_config_option('settings_smtp_port'); $mail->Username = read_config_option('settings_smtp_username'); $mail->Password = read_config_option('settings_smtp_password'); if ($mail->Username != '') { $mail->SMTPAuth = true; } // Set a reasonable timeout of 5 seconds $timeout = read_config_option('settings_smtp_timeout'); if (empty($timeout) || $timeout < 0 || $timeout > 300) { $mail->Timeout = 5; } else { $mail->Timeout = $timeout; } $secure = read_config_option('settings_smtp_secure'); if (!empty($secure) && $secure != 'none') { $smtp->SMTPSecure = $secure; if (substr_count($mail->Host, ':') == 0) { $mail->Host = $secure . '://' . $mail->Host; } } } } } // Set the from information if (!is_array($from)) { $fromname = ''; if ($from == '') { $from = read_config_option('settings_from_email'); $fromname = read_config_option('settings_from_name'); if (isset($_SERVER['HOSTNAME'])) { $from = 'Cacti@' . $_SERVER['HOSTNAME']; } else { $from = '*****@*****.**'; } if ($fromname == '') { $fromname = 'Cacti'; } } $mail->setFrom($from, $fromname); } else { $mail->setFrom($from[0], $from[1]); } if (!is_array($to)) { $to = explode(',', $to); foreach ($to as $t) { $t = trim($t); if ($t != '') { $mail->addAddress($t); } } } else { foreach ($to as $email => $name) { $mail->addAddress($email, $name); } } if (!is_array($cc)) { if ($cc != '') { $cc = explode(',', $cc); foreach ($cc as $c) { $c = trim($c); $mail->addCC($c); } } } else { foreach ($cc as $email => $name) { $mail->addCC($email, $name); } } if (!is_array($bcc)) { if ($bcc != '') { $bcc = explode(',', $bcc); foreach ($bcc as $bc) { $bc = trim($bc); $mail->addBCC($bc); } } } else { foreach ($bcc as $email => $name) { $mail->addBCC($email, $name); } } if (!is_array($replyto)) { if ($replyto != '') { $mail->replyTo($replyto); } } else { $mail->replyTo($replyto[0], $replyto[1]); } // Set the wordwrap limits $wordwrap = read_config_option('settings_wordwrap'); if ($wordwrap == '') { $wordwrap = 76; } if ($wordwrap > 9999) { $wordwrap = 9999; } if ($wordwrap < 0) { $wordwrap = 76; } $mail->WordWrap = $wordwrap; $mail->setWordWrap(); $i = 0; // Handle Graph Attachments if (is_array($attachments) && sizeof($attachments) && substr_count($body, '<GRAPH>') > 0) { foreach ($attachments 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 != '') { /* get content id and create attachment */ $cid = getmypid() . '_' . $i . '@' . 'localhost'; /* attempt to attach */ if ($mail->addStringEmbededImage($data, $cid, $val['filename'] . '.png', 'base64', 'image/png', 'inline') === false) { cacti_log('ERROR: ' . $mail->ErrorInfo, false); return $mail->ErrorInfo; } $body = str_replace('<GRAPH>', "<br><br><img src='cid:{$cid}'>", $body); } else { $body = str_replace('<GRAPH>', "<br><img src='" . $val['file'] . "'><br>Could not open!<br>" . $val['file'], $body); } $i++; } } elseif (is_array($attachments) && sizeof($attachments) && substr_count($body, '<GRAPH:') > 0) { foreach ($attachments as $attachment) { if ($attachment['attachment'] != '') { /* get content id and create attachment */ $cid = getmypid() . '_' . $i . '@' . 'localhost'; /* attempt to attach */ if ($mail->addStringEmbeddedImage($attachment['attachment'], $cid, $attachment['filename'], 'base64', $attachment['mime_type'], $attachment['inline']) === false) { cacti_log('ERROR: ' . $mail->ErrorInfo, false); return $mail->ErrorInfo; } /* handle the body text */ switch ($attachment['inline']) { case 'inline': $body = str_replace('<GRAPH:' . $attachment['graphid'] . ':' . $attachment['timespan'] . '>', "<img border='0' src='cid:{$cid}' >", $body); break; case 'attachment': $body = str_replace('<GRAPH:' . $attachment['graphid'] . ':' . $attachment['timespan'] . '>', '', $body); break; } } else { $body = str_replace('<GRAPH:' . $attachment['graphid'] . ':' . $attachment['timespan'] . '>', "<img border='0' src='" . $attachment['filename'] . "' ><br>Could not open!<br>" . $attachment['filename'], $body); } $i++; } } /* process custom headers */ if (is_array($headers) && sizeof($headers)) { foreach ($headers as $name => $value) { $mail->addCustomHeader($name, $value); } } // Set both html and non-html bodies $text = array('text' => '', 'html' => ''); if ($body_text != '' && $html == true) { $text['html'] = $body . '<br>'; $text['text'] = $body_text; $mail->isHTML(true); $mail->Body = $text['html']; $mail->AltBody = $text['text']; } elseif ($attachments == '' && $html == false) { if ($body_text != '') { $body = $body_text; } else { $body = str_replace('<br>', "\n", $body); $body = str_replace('<BR>', "\n", $body); $body = str_replace('</BR>', "\n", $body); } $text['text'] = strip_tags($body); $mail->isHTML(false); $mail->Body = $text['text']; $mail->AltBody = $text['text']; } else { $text['html'] = $body . '<br>'; $text['text'] = strip_tags(str_replace('<br>', "\n", $body)); $mail->isHTML(true); $mail->Body = $text['html']; $mail->AltBody = $text['text']; } if ($mail->send()) { return ''; } else { return $mail->ErrorInfo; } }