From 8720595874094a8fe6f4aac899506f8978542ce9 Mon Sep 17 00:00:00 2001 From: Simon LEBLANC Date: Wed, 21 Feb 2018 11:07:11 +0100 Subject: [PATCH] Allow setting SMTP config Signed-off-by: Thomas Citharel --- action/add_comment.php | 2 +- action/send_edit_link_by_email_action.php | 4 +- adminstuds.php | 2 +- .../Framadate/Services/MailService.php | 38 ++++++++++++++++--- app/inc/config.template.php | 10 ++++- create_classic_poll.php | 4 +- create_date_poll.php | 2 +- find_polls.php | 8 ++-- studs.php | 2 +- 9 files changed, 54 insertions(+), 18 deletions(-) diff --git a/action/add_comment.php b/action/add_comment.php index 55f98de..df75385 100644 --- a/action/add_comment.php +++ b/action/add_comment.php @@ -42,7 +42,7 @@ $is_admin = false; $logService = new LogService(); $pollService = new PollService($connect, $logService); $inputService = new InputService(); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $notificationService = new NotificationService($mailService); $securityService = new SecurityService(); diff --git a/action/send_edit_link_by_email_action.php b/action/send_edit_link_by_email_action.php index 83d3184..f5f2d54 100644 --- a/action/send_edit_link_by_email_action.php +++ b/action/send_edit_link_by_email_action.php @@ -28,7 +28,7 @@ include_once __DIR__ . '/../app/inc/init.php'; $logService = new LogService(); $sessionService = new SessionService(); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $pollService = new PollService($connect, $logService); $result = false; @@ -91,4 +91,4 @@ $smarty->error_reporting = E_ALL & ~E_NOTICE; $response = ['result' => $result, 'message' => $message]; -echo json_encode($response); \ No newline at end of file +echo json_encode($response); diff --git a/adminstuds.php b/adminstuds.php index b8a155e..356f815 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -49,7 +49,7 @@ $logService = new LogService(); $pollService = new PollService($connect, $logService); $adminPollService = new AdminPollService($connect, $pollService, $logService); $inputService = new InputService(); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $notificationService = new NotificationService($mailService); /* PAGE */ diff --git a/app/classes/Framadate/Services/MailService.php b/app/classes/Framadate/Services/MailService.php index b78325b..9c32f3f 100644 --- a/app/classes/Framadate/Services/MailService.php +++ b/app/classes/Framadate/Services/MailService.php @@ -10,21 +10,26 @@ class MailService { private $smtp_allowed; + private $smtp_options = []; + private $logService; - function __construct($smtp_allowed) { + function __construct($smtp_allowed, $smtp_options = []) { $this->logService = new LogService(); $this->smtp_allowed = $smtp_allowed; + if (true === is_array($smtp_options)) { + $this->smtp_options = $smtp_options; + } } public function isValidEmail($email) { return filter_var($email, FILTER_VALIDATE_EMAIL); } - function send($to, $subject, $body, $msgKey = null) { + public function send($to, $subject, $body, $msgKey = null) { if ($this->smtp_allowed === true && $this->canSendMsg($msgKey)) { $mail = new PHPMailer(true); - $mail->isSMTP(); + $this->configureMailer($mail); // From $mail->FromName = NOMAPPLICATION; @@ -60,7 +65,7 @@ class MailService { } } - function canSendMsg($msgKey) { + public function canSendMsg($msgKey) { if ($msgKey === null) { return true; } @@ -70,5 +75,28 @@ class MailService { } return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || time() - $_SESSION[self::MAILSERVICE_KEY][$msgKey] > self::DELAY_BEFORE_RESEND; } + + /** + * Configure the mailer with the options + * + * @param PHPMailer $mailer + */ + private function configureMailer(PHPMailer $mailer) { + $mailer->isSMTP(); + + $available_options = [ + 'host' => 'Host', + 'auth' => 'SMTPAuth', + 'username' => 'Username', + 'password' => 'Password', + 'secure' => 'SMTPSecure', + 'port' => 'Port', + ]; + + foreach ($available_options as $config_option => $mailer_option) { + if (true === isset($this->smtp_options[$config_option]) && false === empty($this->smtp_options[$config_option])) { + $mailer->{$mailer_option} = $this->smtp_options[$config_option]; + } + } + } } - \ No newline at end of file diff --git a/app/inc/config.template.php b/app/inc/config.template.php index 367d50d..b5a62bf 100644 --- a/app/inc/config.template.php +++ b/app/inc/config.template.php @@ -89,6 +89,14 @@ const TIME_EDIT_LINK_EMAIL = 60; $config = [ /* general config */ 'use_smtp' => true, // use email for polls creation/modification/responses notification + 'smtp_options' => [ + 'host' => 'localhost', // SMTP server (you could add many servers (main and backup for example) : use ";" like separator + 'auth' => false, // Enable SMTP authentication + 'username' => '', // SMTP username + 'password' => '', // SMTP password + 'secure' => '', // Enable encryption (false, tls or ssl) + 'port' => 25, // TCP port to connect to + ], 'tracking_code' => '', // add HTML code to every page, useful for tools like Piwik /* home */ 'show_what_is_that' => true, // display "how to use" section @@ -98,5 +106,5 @@ $config = [ 'default_poll_duration' => 180, // default values for the new poll duration (number of days). /* create_classic_poll.php */ 'user_can_add_img_or_link' => true, // user can add link or URL when creating his poll. - 'markdown_editor_by_default' => true // The markdown editor for the description is enabled by default + 'markdown_editor_by_default' => true, // The markdown editor for the description is enabled by default ]; diff --git a/create_classic_poll.php b/create_classic_poll.php index 6dc1246..5e88774 100644 --- a/create_classic_poll.php +++ b/create_classic_poll.php @@ -29,7 +29,7 @@ include_once __DIR__ . '/app/inc/init.php'; /*---------*/ $logService = new LogService(); $pollService = new PollService($connect, $logService); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $purgeService = new PurgeService($connect, $logService); if (is_file('bandeaux_local.php')) { @@ -44,7 +44,7 @@ if (empty($_SESSION['form']->title) || empty($_SESSION['form']->admin_name) || ( $smarty->assign('error', __('Error', 'You haven\'t filled the first section of the poll creation.')); $smarty->display('error.tpl'); exit; -} +} // Min/Max archive date $min_expiry_time = $pollService->minExpiryDate(); $max_expiry_time = $pollService->maxExpiryDate(); diff --git a/create_date_poll.php b/create_date_poll.php index 05261b0..9b0fa49 100644 --- a/create_date_poll.php +++ b/create_date_poll.php @@ -30,7 +30,7 @@ include_once __DIR__ . '/app/inc/init.php'; /*---------*/ $logService = new LogService(); $pollService = new PollService($connect, $logService); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $purgeService = new PurgeService($connect, $logService); $inputService = new InputService(); diff --git a/find_polls.php b/find_polls.php index c9fa931..fdfc3ca 100644 --- a/find_polls.php +++ b/find_polls.php @@ -4,16 +4,16 @@ * is not distributed with this file, you can obtain one at * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt * - * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ + * Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ * Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft) * * ============================= * - * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence + * Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence * ne se trouve pas avec ce fichier vous pouvez l'obtenir sur * http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt * - * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ + * Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft) */ @@ -28,7 +28,7 @@ include_once __DIR__ . '/app/inc/init.php'; /* -------- */ $logService = new LogService(); $pollService = new PollService($connect, $logService); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); /* PAGE */ /* ---- */ diff --git a/studs.php b/studs.php index b9a30b2..531bf13 100644 --- a/studs.php +++ b/studs.php @@ -57,7 +57,7 @@ $comments = []; $logService = new LogService(); $pollService = new PollService($connect, $logService); $inputService = new InputService(); -$mailService = new MailService($config['use_smtp']); +$mailService = new MailService($config['use_smtp'], $config['smtp_options']); $notificationService = new NotificationService($mailService); $securityService = new SecurityService(); $sessionService = new SessionService();