2016-10-13 15:07:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Vn\Web;
|
|
|
|
|
2016-10-17 06:52:14 +00:00
|
|
|
require_once 'libphp-phpmailer/PHPMailerAutoload.php';
|
2016-10-13 15:07:48 +00:00
|
|
|
|
|
|
|
use Vn\Lib\UserException;
|
|
|
|
|
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
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
$mail = new \PHPMailer();
|
|
|
|
$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);
|
2016-10-13 15:07:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
$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-05-23 10:14:20 +00:00
|
|
|
foreach($mailList as $to)
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|