2014-12-23 00:58:00 +01:00
|
|
|
<?php
|
|
|
|
namespace Framadate\Services;
|
|
|
|
|
2018-07-09 12:17:33 +02:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2015-08-18 18:05:43 +02:00
|
|
|
|
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;
|
|
|
|
|
2018-02-21 11:07:11 +01:00
|
|
|
private $smtp_options = [];
|
|
|
|
|
2015-05-29 17:41:50 +02:00
|
|
|
private $logService;
|
|
|
|
|
2018-02-21 11:07:11 +01:00
|
|
|
function __construct($smtp_allowed, $smtp_options = []) {
|
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;
|
2018-02-21 11:07:11 +01:00
|
|
|
if (true === is_array($smtp_options)) {
|
|
|
|
$this->smtp_options = $smtp_options;
|
|
|
|
}
|
2014-12-23 00:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isValidEmail($email) {
|
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
}
|
|
|
|
|
2018-02-21 11:07:11 +01:00
|
|
|
public 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);
|
2018-02-21 11:07:11 +01:00
|
|
|
$this->configureMailer($mail);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 11:07:11 +01:00
|
|
|
public 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;
|
|
|
|
}
|
2018-02-21 11:07:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 00:58:00 +01:00
|
|
|
}
|