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-05-25 15:50:35 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2018-02-19 00:18:43 +01:00
|
|
|
private $smtp_allowed;
|
|
|
|
|
2018-05-25 15:50:35 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-02-21 11:07:11 +01:00
|
|
|
private $smtp_options = [];
|
|
|
|
|
2018-05-25 15:50:35 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $use_sendmail;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var LogService
|
|
|
|
*/
|
2015-05-29 17:41:50 +02:00
|
|
|
private $logService;
|
|
|
|
|
2018-05-25 15:50:35 +02:00
|
|
|
/**
|
|
|
|
* MailService constructor.
|
|
|
|
* @param $smtp_allowed
|
|
|
|
* @param array $smtp_options
|
|
|
|
* @param bool $use_sendmail
|
|
|
|
*/
|
|
|
|
public function __construct($smtp_allowed, $smtp_options = [], $use_sendmail = false) {
|
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;
|
|
|
|
}
|
2018-05-25 15:50:35 +02:00
|
|
|
$this->use_sendmail = $use_sendmail;
|
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) {
|
2018-05-25 15:50:35 +02:00
|
|
|
if ($this->use_sendmail) {
|
|
|
|
$mailer->isSendmail();
|
|
|
|
} else {
|
|
|
|
$mailer->isSMTP();
|
|
|
|
}
|
2018-02-21 11:07:11 +01:00
|
|
|
|
|
|
|
$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
|
|
|
}
|