2016-10-13 15:07:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
2019-09-05 16:16:10 +00:00
|
|
|
require_once 'libphp-phpmailer/autoload.php';
|
2016-10-13 15:07:48 +00:00
|
|
|
|
|
|
|
use Vn\Lib\UserException;
|
2023-08-21 09:41:51 +00:00
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
2016-10-13 15:07:48 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class Mailer {
|
2017-11-29 10:01:48 +00:00
|
|
|
private $conf;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function __construct($db) {
|
|
|
|
$this->conf = $db->getObject(
|
2017-12-20 11:34:04 +00:00
|
|
|
'SELECT host, port, secure, sender, senderName, user, password
|
|
|
|
FROM hedera.mailConfig'
|
2016-10-13 15:07:48 +00:00
|
|
|
);
|
2017-11-29 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function createObject($mailTo, $body, $subject) {
|
2017-11-29 10:01:48 +00:00
|
|
|
$conf = $this->conf;
|
2016-10-13 15:07:48 +00:00
|
|
|
|
2023-08-21 09:41:51 +00:00
|
|
|
$mail = new PHPMailer();
|
2018-05-23 10:14:20 +00:00
|
|
|
$mail->isSMTP();
|
2017-11-29 10:01:48 +00:00
|
|
|
$mail->Host = $conf->host;
|
2016-10-13 15:07:48 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!empty($conf->user)) {
|
2016-10-13 15:07:48 +00:00
|
|
|
$mail->SMTPAuth = TRUE;
|
2017-11-29 10:01:48 +00:00
|
|
|
$mail->Username = $conf->user;
|
2018-05-23 10:14:20 +00:00
|
|
|
$mail->Password = base64_decode($conf->password);
|
2018-05-23 11:09:55 +00:00
|
|
|
} else
|
2016-10-13 15:07:48 +00:00
|
|
|
$mail->SMTPAuth = FALSE;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if ($conf->secure) {
|
2016-10-13 15:07:48 +00:00
|
|
|
$mail->SMTPSecure = 'ssl';
|
|
|
|
$mail->Port = 465;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$mail->setFrom($conf->sender, $conf->senderName);
|
|
|
|
$mail->IsHTML(TRUE);
|
2017-11-29 10:01:48 +00:00
|
|
|
$mail->Subject = $subject;
|
|
|
|
$mail->Body = $body;
|
|
|
|
$mail->CharSet = 'UTF-8';
|
2017-04-24 13:50:05 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$mailList = explode(',', $mailTo);
|
2017-04-24 13:50:05 +00:00
|
|
|
|
2018-06-06 11:08:17 +00:00
|
|
|
foreach ($mailList as $to)
|
2018-05-23 10:14:20 +00:00
|
|
|
$mail->AddAddress($to);
|
2016-10-13 15:07:48 +00:00
|
|
|
|
2017-11-29 10:01:48 +00:00
|
|
|
return $mail;
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
function send($mailTo, $body, $subject) {
|
|
|
|
$mail = $this->createObject($mailTo, $body, $subject);
|
2016-10-13 15:07:48 +00:00
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
if (!$mail->Send())
|
|
|
|
throw new UserException('Send error: '.$mail->ErrorInfo);
|
2016-10-13 15:07:48 +00:00
|
|
|
}
|
|
|
|
}
|