Write log when send a mail
This commit is contained in:
parent
8313b9345d
commit
278e478d1a
@ -9,7 +9,10 @@ class MailService {
|
||||
|
||||
const MAILSERVICE_KEY = 'mailservice';
|
||||
|
||||
private $logService;
|
||||
|
||||
function __construct($smtp_allowed) {
|
||||
$this->logService = new LogService();
|
||||
$this->smtp_allowed = $smtp_allowed;
|
||||
}
|
||||
|
||||
@ -51,11 +54,7 @@ class MailService {
|
||||
|
||||
// Send mail
|
||||
|
||||
mail($to, $subject, $body, $headers, $param);
|
||||
|
||||
// Set date before resend in sessions
|
||||
|
||||
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time() + self::DELAY_BEFORE_RESEND;
|
||||
$this->sendMail($to, $subject, $body, $param, $msgKey, $headers);
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +66,19 @@ class MailService {
|
||||
if (!isset($_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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ class MailServiceUnitTest extends FramaTestCase {
|
||||
function should_send_a_2nd_mail_after_a_good_interval() {
|
||||
// Given
|
||||
$mailService = new MailService(true);
|
||||
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() - 500];
|
||||
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() - 1000];
|
||||
|
||||
// When
|
||||
$canSendMsg = $mailService->canSendMsg(self::MSG_KEY);
|
||||
@ -27,7 +27,7 @@ class MailServiceUnitTest extends FramaTestCase {
|
||||
function should_not_send_2_mails_in_a_short_interval() {
|
||||
// Given
|
||||
$mailService = new MailService(true);
|
||||
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time() + 500];
|
||||
$_SESSION[MailService::MAILSERVICE_KEY] = [self::MSG_KEY => time()];
|
||||
|
||||
// When
|
||||
$canSendMsg = $mailService->canSendMsg(self::MSG_KEY);
|
||||
|
@ -83,7 +83,7 @@ function sendUpdateNotification($poll, $mailService, $name, $type) {
|
||||
$message .= Utils::getUrlSondage($poll->admin_id, true) . "\n\n";
|
||||
|
||||
$messageTypeKey = $type . '-' . $poll->id;
|
||||
$mailService->send($messageTypeKey, $poll->admin_mail, $subject, $message);
|
||||
$mailService->send($poll->admin_mail, $subject, $message, '', $messageTypeKey);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user