From 95dfe4f55c38be0bfc3401f2078fc30f2737adc9 Mon Sep 17 00:00:00 2001 From: jim-p Date: Thu, 13 Jun 2013 12:51:48 -0400 Subject: Split actual SMTP send into its own function. --- etc/inc/notices.inc | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'etc/inc') diff --git a/etc/inc/notices.inc b/etc/inc/notices.inc index 11d4176..ff45054 100644 --- a/etc/inc/notices.inc +++ b/etc/inc/notices.inc @@ -273,12 +273,6 @@ function notify_via_smtp($message, $force = false) { if(isset($config['notifications']['smtp']['disable']) && !$force) return; - if(!$config['notifications']['smtp']['ipaddress']) - return; - - if(!$config['notifications']['smtp']['notifyemailaddress']) - return; - /* Do NOT send the same message twice */ if(file_exists("/var/db/notices_lastmsg.txt")) { $lastmsg = trim(file_get_contents("/var/db/notices_lastmsg.txt")); @@ -286,9 +280,26 @@ function notify_via_smtp($message, $force = false) { return; } + /* Store last message sent to avoid spamming */ + $fd = fopen("/var/db/notices_lastmsg.txt", "w"); + fwrite($fd, $message); + fclose($fd); + + send_smtp_message($message, "{$config['system']['hostname']}.{$config['system']['domain']} - Notification"); + return; +} + +function send_smtp_message($message, $subject = "(no subject)") { + global $config, $g; require_once("sasl.inc"); require_once("smtp.inc"); + if(!$config['notifications']['smtp']['ipaddress']) + return; + + if(!$config['notifications']['smtp']['notifyemailaddress']) + return; + $smtp = new smtp_class; $from = "pfsense@{$config['system']['hostname']}.{$config['system']['domain']}"; @@ -317,15 +328,10 @@ function notify_via_smtp($message, $force = false) { $headers = array( "From: {$from}", "To: {$to}", - "Subject: {$config['system']['hostname']}.{$config['system']['domain']} - Notification", + "Subject: {$subject}", "Date: ".date("r") ); - /* Store last message sent to avoid spamming */ - $fd = fopen("/var/db/notices_lastmsg.txt", "w"); - fwrite($fd, $message); - fclose($fd); - if($smtp->SendMessage($from, preg_split('/\s*,\s*/', trim($to)), $headers, $message)) { log_error(sprintf(gettext("Message sent to %s OK"), $to)); return; @@ -333,10 +339,8 @@ function notify_via_smtp($message, $force = false) { log_error(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error)); return(sprintf(gettext('Could not send the message to %1$s -- Error: %2$s'), $to, $smtp->error)); } - } - /****f* notices/notify_via_growl * NAME * notify_via_growl -- cgit v1.1