Write log when send a mail

This commit is contained in:
Olivier PEREZ 2015-05-29 17:41:50 +02:00
parent 8313b9345d
commit 278e478d1a
3 changed files with 20 additions and 9 deletions

View File

@ -9,7 +9,10 @@ class MailService {
const MAILSERVICE_KEY = 'mailservice'; const MAILSERVICE_KEY = 'mailservice';
private $logService;
function __construct($smtp_allowed) { function __construct($smtp_allowed) {
$this->logService = new LogService();
$this->smtp_allowed = $smtp_allowed; $this->smtp_allowed = $smtp_allowed;
} }
@ -51,11 +54,7 @@ class MailService {
// Send mail // Send mail
mail($to, $subject, $body, $headers, $param); $this->sendMail($to, $subject, $body, $param, $msgKey, $headers);
// Set date before resend in sessions
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time() + self::DELAY_BEFORE_RESEND;
} }
} }
@ -67,7 +66,19 @@ class MailService {
if (!isset($_SESSION[self::MAILSERVICE_KEY])) { if (!isset($_SESSION[self::MAILSERVICE_KEY])) {
$_SESSION[self::MAILSERVICE_KEY] = []; $_SESSION[self::MAILSERVICE_KEY] = [];
} }
return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || $_SESSION[self::MAILSERVICE_KEY][$msgKey] < time(); return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || time() - $_SESSION[self::MAILSERVICE_KEY][$msgKey] > self::DELAY_BEFORE_RESEND;
}
private function sendMail($to, $subject, $body, $param, $msgKey, $headers) {
mail($to, $subject, $body, $headers, $param);
// Log
$this->logService->log('MAIL', 'Mail sent to: ' . $to . ', key: ' . $msgKey);
// Store the mail sending date
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time();
} }
} }

View File

@ -12,7 +12,7 @@ class MailServiceUnitTest extends FramaTestCase {
function should_send_a_2nd_mail_after_a_good_interval() { function should_send_a_2nd_mail_after_a_good_interval() {
// Given // Given
$mailService = new MailService(true); $mailService = new MailService(true);
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() - 500]; $_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() - 1000];
// When // When
$canSendMsg = $mailService->canSendMsg(self::MSG_KEY); $canSendMsg = $mailService->canSendMsg(self::MSG_KEY);
@ -27,7 +27,7 @@ class MailServiceUnitTest extends FramaTestCase {
function should_not_send_2_mails_in_a_short_interval() { function should_not_send_2_mails_in_a_short_interval() {
// Given // Given
$mailService = new MailService(true); $mailService = new MailService(true);
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() + 500]; $_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time()];
// When // When
$canSendMsg = $mailService->canSendMsg(self::MSG_KEY); $canSendMsg = $mailService->canSendMsg(self::MSG_KEY);

View File

@ -83,7 +83,7 @@ function sendUpdateNotification($poll, $mailService, $name, $type) {
$message .= Utils::getUrlSondage($poll->admin_id, true) . "\n\n"; $message .= Utils::getUrlSondage($poll->admin_id, true) . "\n\n";
$messageTypeKey = $type . '-' . $poll->id; $messageTypeKey = $type . '-' . $poll->id;
$mailService->send($messageTypeKey, $poll->admin_mail, $subject, $message); $mailService->send($poll->admin_mail, $subject, $message, '', $messageTypeKey);
} }
} }