2014-12-23 00:58:00 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
use PHPMailer;
|
|
|
|
|
2014-12-23 00:58:00 +01:00
|
|
|
class MailService {
|
2015-05-29 17:06:03 +02:00
|
|
|
const DELAY_BEFORE_RESEND = 300;
|
|
|
|
|
|
|
|
const MAILSERVICE_KEY = 'mailservice';
|
|
|
|
|
2018-02-19 00:18:43 +01:00
|
|
|
private $smtp_allowed;
|
|
|
|
|
2015-05-29 17:41:50 +02:00
|
|
|
private $logService;
|
|
|
|
|
2014-12-23 00:58:00 +01:00
|
|
|
function __construct($smtp_allowed) {
|
2015-05-29 17:41:50 +02:00
|
|
|
$this->logService = new LogService();
|
2014-12-23 00:58:00 +01:00
|
|
|
$this->smtp_allowed = $smtp_allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValidEmail($email) {
|
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:49:01 +02:00
|
|
|
function send($to, $subject, $body, $msgKey = null) {
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($this->smtp_allowed === true && $this->canSendMsg($msgKey)) {
|
2015-08-18 18:05:43 +02:00
|
|
|
$mail = new PHPMailer(true);
|
|
|
|
$mail->isSMTP();
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// From
|
|
|
|
$mail->FromName = NOMAPPLICATION;
|
|
|
|
$mail->From = ADRESSEMAILADMIN;
|
|
|
|
if ($this->isValidEmail(ADRESSEMAILREPONSEAUTO)) {
|
|
|
|
$mail->addReplyTo(ADRESSEMAILREPONSEAUTO);
|
|
|
|
}
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// To
|
|
|
|
$mail->addAddress($to);
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// Subject
|
|
|
|
$mail->Subject = $subject;
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// Bodies
|
|
|
|
$body = $body . ' <br/><br/>' . __('Mail', 'Thanks for your trust.') . ' <br/>' . NOMAPPLICATION . ' <hr/>' . __('Mail', 'FOOTER');
|
|
|
|
$mail->isHTML(true);
|
|
|
|
$mail->msgHTML($body, ROOT_DIR, true);
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// Build headers
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
$mail->addCustomHeader('Auto-Submitted', 'auto-generated');
|
|
|
|
$mail->addCustomHeader('Return-Path', '<>');
|
2014-12-23 00:58:00 +01:00
|
|
|
|
2015-05-29 17:06:03 +02:00
|
|
|
// Send mail
|
2015-08-18 18:05:43 +02:00
|
|
|
$mail->send();
|
|
|
|
|
|
|
|
// Log
|
|
|
|
$this->logService->log('MAIL', 'Mail sent to: ' . $to . ', key: ' . $msgKey);
|
2015-05-29 17:06:03 +02:00
|
|
|
|
2015-08-18 18:05:43 +02:00
|
|
|
// Store the mail sending date
|
|
|
|
$_SESSION[self::MAILSERVICE_KEY][$msgKey] = time();
|
2015-05-29 17:06:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function canSendMsg($msgKey) {
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($msgKey === null) {
|
2015-05-29 17:06:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($_SESSION[self::MAILSERVICE_KEY])) {
|
|
|
|
$_SESSION[self::MAILSERVICE_KEY] = [];
|
2014-12-23 00:58:00 +01:00
|
|
|
}
|
2015-05-29 17:41:50 +02:00
|
|
|
return !isset($_SESSION[self::MAILSERVICE_KEY][$msgKey]) || time() - $_SESSION[self::MAILSERVICE_KEY][$msgKey] > self::DELAY_BEFORE_RESEND;
|
|
|
|
}
|
2014-12-23 00:58:00 +01:00
|
|
|
}
|
|
|
|
|