1<?php 2 3/** 4 * Observium 5 * 6 * This file is part of Observium. 7 * 8 * @package observium 9 * @subpackage alerting 10 * @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited 11 * 12 */ 13 14// Find local hostname 15$localhost = get_localhost(); 16 17$emails = array(); 18 19$emails[$endpoint['email']] = $endpoint['contact_descr']; 20 21// Mail backend params 22$backend = strtolower(trim($config['email']['backend'])); 23switch ($backend) 24{ 25 case 'sendmail': 26 $mail_params['sendmail_path'] = $config['email']['sendmail_path']; 27 break; 28 case 'smtp': 29 $mail_params['host'] = $config['email']['smtp_host']; 30 $mail_params['port'] = $config['email']['smtp_port']; 31 if ($config['email']['smtp_secure'] == 'ssl') 32 { 33 $mail_params['host'] = 'ssl://'.$config['email']['smtp_host']; 34 if ($config['email']['smtp_port'] == 25) 35 { 36 $mail_params['port'] = 465; // Default port for SSL 37 } 38 } 39 $mail_params['timeout'] = $config['email']['smtp_timeout']; 40 $mail_params['auth'] = $config['email']['smtp_auth']; 41 $mail_params['username'] = $config['email']['smtp_username']; 42 $mail_params['password'] = $config['email']['smtp_password']; 43 $mail_params['localhost'] = $localhost; 44 if (OBS_DEBUG) { $mail_params['debug'] = TRUE; } 45 break; 46 case 'mail': 47 default: 48 $backend = 'mail'; // Default mailer backend 49} 50 51// Time sent RFC 2822 52$time_rfc = date('r', time()); 53 54// Mail headers 55$headers = array(); 56if (empty($config['email']['from'])) 57{ 58 // Default "From:" 59 $headers['From'] = 'Observium <observium@'.$localhost.'>'; 60 $headers['Return-Path'] = 'observium@'.$localhost; 61} else { 62 // Validate configured mail from 63 foreach (parse_email($config['email']['from']) as $from => $from_name) 64 { 65 $headers['From'] = (empty($from_name) ? $from : '"'.$from_name.'" <'.$from.'>'); // From: 66 $headers['Return-Path'] = $from; 67 break; // use only first entry 68 } 69} 70 71$rcpts = array(); 72$rcpts_full = array(); 73foreach ($emails as $to => $to_name) 74{ 75 $rcpts_full[] = (empty($to_name) ? $to : '"'.trim($to_name).'" <'.$to.'>'); 76 $rcpts[] = $to; 77} 78 79$rcpts_full = implode(', ', $rcpts_full); 80$rcpts = implode(', ', $rcpts); 81 82$headers['To'] = $rcpts_full; // To: 83$headers['Subject'] = $message_tags['TITLE']; // Subject: 84// ID and Date, leave it before X- headers 85$headers['Message-ID'] = '<' . md5(uniqid(time())) . '@' . $localhost . '>'; 86$headers['Date'] = $time_rfc; 87 88$headers['X-Priority'] = 3; // Mail priority 89$headers['X-Mailer'] = OBSERVIUM_PRODUCT.' '.OBSERVIUM_VERSION; // X-Mailer: 90 91// Mail autogenerated, suppress autorespond by any issue system 92$headers['Precedence'] = 'bulk'; 93$headers['Auto-submitted'] = 'auto-generated'; 94$headers['X-Auto-Response-Suppress'] = 'All'; 95 96$time_sent = $time_rfc; 97 98// Creating the Mime message 99$mime = new Mail_mime(array('head_charset' => 'utf-8', 100 'text_charset' => 'utf-8', 101 'html_charset' => 'utf-8', 102 'eol' => PHP_EOL)); 103 104// Generate Mail text in both plain text and html 105$message['text'] = simple_template('email_text', $message_tags, array('is_file' => TRUE)); 106 107$message_tags_html = $message_tags; 108$message_tags_html['CONDITIONS'] = nl2br(escape_html($message_tags['CONDITIONS'])); 109$message_tags_html['METRICS'] = nl2br(escape_html($message_tags['METRICS'])); 110 111// Generate image attach 112//$graphs = json_decode($message_tags_html['ENTITY_GRAPHS_ARRAY'], TRUE); 113$graphs = $message_tags_html['ENTITY_GRAPHS_ARRAY']; 114 115if (is_array($graphs) && count($graphs)) 116{ 117 $message_tags_html['ENTITY_GRAPHS'] = ''; // Reinit graphs html 118 foreach ($graphs as $key => $graph) 119 { 120 $cid = generate_random_string(16); 121 // Unencode data uri tag to file content 122 list($gmime, $base64) = explode(';', $graph['data'], 2); 123 $gmime = substr($gmime, 5); 124 $base64 = substr($base64, 7); 125 //print_vars(substr($graph['data'], 0, 20)); 126 //print_vars($gmime); 127 //print_vars(substr($base64, 0, 20)); 128 $mime->addHTMLImage(base64_decode($base64), $gmime, $cid.'.png', FALSE, $cid); 129 130 $message_tags_html['ENTITY_GRAPHS'] .= '<h4>'.$graph['type'].'</h4>'; 131 $message_tags_html['ENTITY_GRAPHS'] .= '<a href="'.$graph['url'].'"><img src="cid:'.$cid.'"></a><br />'; 132 } 133} 134//print_vars($message_tags_html); 135 136 137 138$message['html'] = simple_template('email_html', $message_tags_html, array('is_file' => TRUE)); 139unset($message_tags_html); 140 141foreach ($message as $part => $part_body) 142{ 143 switch ($part) 144 { 145 case 'text': 146 case 'txt': 147 case 'plain': 148 $part_body .= "\n\nE-mail sent to: $rcpts\n"; 149 $part_body .= "E-mail sent at: $time_sent\n\n"; 150 $part_body .= "-- \n" . OBSERVIUM_PRODUCT_LONG . ' ' . OBSERVIUM_VERSION . "\n" . OBSERVIUM_URL . "\n"; 151 $mime->setTXTBody($part_body); 152 break; 153 case 'html': 154 $part_footer = "\n<br /><p style=\"font-size: 11px;\">E-mail sent to: $rcpts<br />\n"; 155 $part_footer .= "E-mail sent at: $time_sent</p>\n"; 156 $part_footer .= '<div style="font-size: 11px; color: #999;">-- <br /><a href="'.OBSERVIUM_URL.'">'.OBSERVIUM_PRODUCT_LONG.' '.OBSERVIUM_VERSION."</a></div>\n"; 157 if (stripos($part_body, '</body>')) 158 { 159 $part_body = str_ireplace('</body>', $part_footer.'</body>', $part_body); 160 } else { 161 $part_body .= $part_footer; 162 } 163 $mime->setHTMLBody($part_body); 164 break; 165 //case 'image': 166 // break; 167 //case 'attachment': 168 // break; 169 } 170} 171$body = $mime->get(); 172 173// Prepare headers 174foreach ($headers as $name => $value) 175{ 176 $headers[$name] = $mime->encodeHeader($name, $value, 'utf-8', 'quoted-printable'); 177} 178$headers = $mime->headers($headers); 179//var_dump($headers); 180 181// Create mailer instance 182$mail =& Mail::factory($backend, $mail_params); 183// Sending email 184$status = $mail->send($rcpts, $headers, $body); 185 186if (PEAR::isError($status)) 187{ 188 print_message('%rMailer Error%n: ' . $status->getMessage(), 'color'); 189 $notify_status['success'] = FALSE; 190 $notify_status['error'] = $status->getMessage(); 191} else { 192 $notify_status['success'] = TRUE; 193} 194 195unset($message); 196 197// EOF 198