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-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
2018-07-06 18:06:35 +02:00
$body = $body . ' <br/><br/>' . __ ( 'Mail' , 'Thank you for your trust.' ) . ' <br/>' . NOMAPPLICATION . ' <hr/>' . __ ( 'Mail' , " \" The road is long, but the way is clear… \" <br/>Framasoft lives only by your donations.<br/>Thank you in advance for your support https://soutenir.framasoft.org " );
2015-08-18 18:05:43 +02:00
$mail -> isHTML ( true );
2019-04-15 17:53:53 +02:00
$mail -> CharSet = PHPMailer :: CHARSET_UTF8 ;
2019-04-15 18:24:10 +02:00
$mail -> msgHTML ( $body , ROOT_DIR , function ( $html ) use ( $mail ) {
return $this -> html2text ( $mail , $html );
});
2014-12-23 00:58:00 +01:00
2015-08-18 18:05:43 +02:00
// Build headers
$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 ];
}
}
}
2019-04-15 18:24:10 +02:00
/**
* Custom " advanced " callback to pass to msgHTML function
*
* @ param PHPMailer $mailer a PHPMailer instance
* @ param string $html the HTML body of an email
*/
private function html2text ( PHPMailer $mailer , $html ) {
$html = preg_replace ( '/<a[^>]*href="([^"]+)"[^>]*>(.*?)<\/a>/si' , '${2}: ${1}' , $html );
return $mailer -> html2text ( $html );
}
2014-12-23 00:58:00 +01:00
}