issue #14: adding SMTP support and upgrading phpmailer

This commit is contained in:
Pepijn Over 2014-02-11 18:16:00 +01:00
parent 8b3c7f4ef5
commit 9484339842
15 changed files with 4262 additions and 1648 deletions

View File

@ -5,6 +5,8 @@
#
#########################
- Switched from mysql_* to PDO.
- Added SMTP support.
- Updated PHPMailer package to v5.2.6.
#########################
#

View File

@ -267,6 +267,46 @@ function psm_check_updates() {
return false;
}
/**
* Prepare a new Mailer util.
*
* If the from name and email are left blank they will be prefilled from the config.
* @param string $from_name
* @param string $from_email
* @return \psm\Util\Mailer
*/
function psm_build_mail($from_name = null, $from_email = null) {
$phpmailer = new \psm\Util\Mailer();
$phpmailer->Encoding = "base64";
$phpmailer->SMTPDebug = false;
if(psm_get_conf('email_smtp') == '1') {
$phpmailer->IsSMTP();
$phpmailer->Host = psm_get_conf('email_smtp_host');
$phpmailer->Port = psm_get_conf('email_smtp_port');
$smtp_user = psm_get_conf('email_smtp_username');
$smtp_pass = psm_get_conf('email_smtp_password');
if($smtp_user != '' && $smtp_pass != '') {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = $smtp_user;
$phpmailer->Password = $smtp_pass;
}
} else {
$phpmailer->IsMail();
}
if($from_name == null) {
$from_name = psm_get_conf('email_from_name');
}
if($from_email == null) {
$from_email = psm_get_conf('email_from_email');
}
$phpmailer->SetFrom($from_email, $from_name);
return $phpmailer;
}
###############################################
#
# Debug functions

View File

@ -95,6 +95,12 @@ $sm_lang = array(
'email_status' => 'Habilitar envio de email?',
'email_from_email' => 'Endereço do envio de email',
'email_from_name' => 'Nome do envio de email',
'email_smtp' => 'Enable SMTP',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP port',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'Habilitar o envio de mensagem de texto?',
'sms_gateway' => 'Gateway para o uso de envio de mensagens',
'sms_gateway_mosms' => 'Mosms',

View File

@ -95,6 +95,12 @@ $sm_lang = array(
'email_status' => 'Email senden erlauben?',
'email_from_email' => 'Email from address',
'email_from_name' => 'Email from name',
'email_smtp' => 'Enable SMTP',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP port',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'SMS Nachricht senden erlauben?',
'sms_gateway' => 'SMS Gateway',
'sms_gateway_mosms' => 'Mosms',

View File

@ -95,6 +95,12 @@ $sm_lang = array(
'email_status' => 'Allow sending email?',
'email_from_email' => 'Email from address',
'email_from_name' => 'Email from name',
'email_smtp' => 'Enable SMTP',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP port',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'Allow sending text messages?',
'sms_gateway' => 'Gateway to use for sending messages',
'sms_gateway_mosms' => 'Mosms',

View File

@ -95,6 +95,12 @@ $sm_lang = array(
'email_status' => 'Autoriser l envoi de mail?',
'email_from_email' => 'Expéditeur',
'email_from_name' => 'Nom de l expéditeur',
'email_smtp' => 'Enable SMTP',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP port',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'Autoriser l envoi de SMS?',
'sms_gateway' => 'Passerelle à utiliser pour l envoi de SMS',
'sms_gateway_mosms' => 'Mosms',

View File

@ -96,6 +96,12 @@ $sm_lang = array(
'email_status' => '메일전송 허용',
'email_from_email' => 'Email 주소',
'email_from_name' => 'Email 사용자',
'email_smtp' => 'Enable SMTP',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP port',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'SMS전송 허용',
'sms_gateway' => '메세지 전송을 위한 게이트웨이 허용',
'sms_gateway_mosms' => 'Mosms',

View File

@ -95,6 +95,12 @@ $sm_lang = array(
'email_status' => 'Sta email berichten toe?',
'email_from_email' => 'Email van adres',
'email_from_name' => 'Email van naam',
'email_smtp' => 'SMTP gebruiken',
'email_smtp_host' => 'SMTP host',
'email_smtp_port' => 'SMTP poort',
'email_smtp_username' => 'SMTP gebruikersnaam',
'email_smtp_password' => 'SMTP wachtwoord',
'email_smtp_noauth' => 'Laat leeg voor geen authenticatie',
'sms_status' => 'Sta SMS berichten toe?',
'sms_gateway' => 'Gateway voor het sturen van SMS',
'sms_gateway_mosms' => 'Mosms',

View File

@ -31,6 +31,36 @@ use psm\Service\Template;
class Config extends AbstractModule {
/**
* Checkboxes
* @var array $checkboxes
*/
protected $checkboxes = array(
'email_status',
'email_smtp',
'sms_status',
'log_status',
'log_email',
'log_sms',
'show_update',
);
/**
* Fields for saving
* @var array $fields
*/
protected $fields = array(
'email_from_name',
'email_from_email',
'email_smtp_host',
'email_smtp_port',
'email_smtp_username',
'email_smtp_password',
'sms_gateway_username',
'sms_gateway_password',
'sms_from',
);
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
@ -74,25 +104,23 @@ class Config extends AbstractModule {
}
$this->tpl->addTemplateDataRepeat($this->getTemplateId(), 'languages', $languages);
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'email_status_checked' => ($config['email_status'] == '1') ? 'checked="checked"' : '',
'email_from_name' => $config['email_from_name'],
'email_from_email' => $config['email_from_email'],
'sms_status_checked' => ($config['sms_status'] == '1') ? 'checked="checked"' : '',
'sms_selected_' . $config['sms_gateway'] => 'selected="selected"',
'sms_gateway_username' => $config['sms_gateway_username'],
'sms_gateway_password' => $config['sms_gateway_password'],
'sms_from' => $config['sms_from'],
'alert_type_selected_' . $config['alert_type'] => 'selected="selected"',
'log_status_checked' => ($config['log_status'] == '1') ? 'checked="checked"' : '',
'log_email_checked' => ($config['log_email'] == '1') ? 'checked="checked"' : '',
'log_sms_checked' => ($config['log_sms'] == '1') ? 'checked="checked"' : '',
'show_update_checked' => ($config['show_update'] == '1') ? 'checked="checked"' : '',
'auto_refresh_servers' => (isset($config['auto_refresh_servers'])) ? $config['auto_refresh_servers'] : '0',
)
$tpl_data = array(
'sms_selected_' . $config['sms_gateway'] => 'selected="selected"',
'alert_type_selected_' . $config['alert_type'] => 'selected="selected"',
'auto_refresh_servers' => (isset($config['auto_refresh_servers'])) ? $config['auto_refresh_servers'] : '0',
);
foreach($this->checkboxes as $input_key) {
$tpl_data[$input_key . '_checked'] =
(isset($config[$input_key]) && (int) $config[$input_key] == 1)
? 'checked="checked"'
: '';
}
foreach($this->fields as $input_key) {
$tpl_data[$input_key] = (isset($config[$input_key])) ? $config[$input_key] : '';
}
$this->tpl->addTemplateData($this->getTemplateId(), $tpl_data);
}
/**
@ -104,21 +132,18 @@ class Config extends AbstractModule {
// save new config
$clean = array(
'language' => $_POST['language'],
'show_update' => (isset($_POST['show_update'])) ? '1' : '0',
'email_status' => (isset($_POST['email_status'])) ? '1' : '0',
'email_from_name' => $_POST['email_from_name'],
'email_from_email' => $_POST['email_from_email'],
'sms_status' => (isset($_POST['sms_status'])) ? '1' : '0',
'sms_gateway' => $_POST['sms_gateway'],
'sms_gateway_username' => $_POST['sms_gateway_username'],
'sms_gateway_password' => $_POST['sms_gateway_password'],
'sms_from' => $_POST['sms_from'],
'alert_type' => $_POST['alert_type'],
'log_status' => (isset($_POST['log_status'])) ? '1' : '0',
'log_email' => (isset($_POST['log_email'])) ? '1' : '0',
'log_sms' => (isset($_POST['log_sms'])) ? '1' : '0',
'auto_refresh_servers' => (isset($_POST['auto_refresh_servers'])) ? intval($_POST['auto_refresh_servers']) : '0',
);
foreach($this->checkboxes as $input_key) {
$clean[$input_key] = (isset($_POST[$input_key])) ? '1': '0';
}
foreach($this->fields as $input_key) {
if(isset($_POST[$input_key])) {
$clean[$input_key] = $_POST[$input_key];
}
}
// save all values to the database
foreach($clean as $key => $value) {
@ -167,6 +192,12 @@ class Config extends AbstractModule {
'label_email_status' => psm_get_lang('config', 'email_status'),
'label_email_from_email' => psm_get_lang('config', 'email_from_email'),
'label_email_from_name' => psm_get_lang('config', 'email_from_name'),
'label_email_smtp' => psm_get_lang('config', 'email_smtp'),
'label_email_smtp_host' => psm_get_lang('config', 'email_smtp_host'),
'label_email_smtp_port' => psm_get_lang('config', 'email_smtp_port'),
'label_email_smtp_username' => psm_get_lang('config', 'email_smtp_username'),
'label_email_smtp_password' => psm_get_lang('config', 'email_smtp_password'),
'label_email_smtp_noauth' => psm_get_lang('config', 'email_smtp_noauth'),
'label_sms_status' => psm_get_lang('config', 'sms_status'),
'label_sms_gateway' => psm_get_lang('config', 'sms_gateway'),
'label_sms_gateway_mosms' => psm_get_lang('config', 'sms_gateway_mosms'),

57
src/psm/Util/Mailer.class.php Executable file
View File

@ -0,0 +1,57 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @link http://www.phpservermonitor.org/
* @since phpservermon 2.2.0
**/
namespace psm\Util;
/**
* PHPMailer is not using namespaces so unable to load files in autoloader.
*/
require_once(PSM_PATH_VENDOR . '/PHPMailer/class.phpmailer.php');
require_once(PSM_PATH_VENDOR . '/PHPMailer/class.smtp.php');
/**
* PSM Mailer utility
*
* The PHPMailer is an open source lib that can be found in vendor/PHPMailer.
*
* @see \PHPMailer
*/
class Mailer extends \PHPMailer {
/**
* Open new PHPMailer
*
* @param boolean $exceptions
*/
function __construct($exceptions = false) {
parent::__construct($exceptions);
}
}
?>

View File

@ -260,10 +260,7 @@ class Status {
}
// build mail object with some default values
$mail = new \phpmailer();
$mail->From = psm_get_conf('email_from_email');
$mail->FromName = psm_get_conf('email_from_name');
$mail = psm_build_mail();
$mail->Subject = psm_parse_msg($this->status_new, 'email_subject', $this->server);
$mail->Priority = 1;

View File

@ -75,6 +75,31 @@
<input type="text" id="email_from_email" name="email_from_email" value="{email_from_email}" maxlength="255" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email_smtp">{label_email_smtp}</label>
<div class="controls">
<input type="checkbox" id="email_smtp" name="email_smtp[]" {email_smtp_checked} />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email_smtp_host">{label_email_smtp_host}</label>
<div class="controls">
<input type="text" id="email_smtp_host" name="email_smtp_host" value="{email_smtp_host}" maxlength="100" placeholder="{label_email_smtp_host}" />
<input type="text" class="input-small" id="email_smtp_port" name="email_smtp_port" value="{email_smtp_port}" maxlength="10" placeholder="{label_email_smtp_port}" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email_smtp_username">{label_email_smtp_username}</label>
<div class="controls">
<input type="text" id="email_smtp_username" name="email_smtp_username" value="{email_smtp_username}" maxlength="100" placeholder="{label_email_smtp_noauth}" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email_smtp_password">{label_email_smtp_password}</label>
<div class="controls">
<input type="password" id="email_smtp_password" name="email_smtp_password" value="{email_smtp_password}" maxlength="100" placeholder="{label_email_smtp_noauth}" />
</div>
</div>
<div class="form-actions">
<button class="btn btn-success" type="submit">{label_save}</button>
</div>

2949
vendor/PHPMailer/class.phpmailer.php vendored Executable file

File diff suppressed because it is too large Load Diff

1092
vendor/PHPMailer/class.smtp.php vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff