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;
|
|
|
|
|
|
|
|
class Mailer
|
|
|
|
{
|
2017-11-29 10:01:48 +00:00
|
|
|
private $conf;
|
|
|
|
|
|
|
|
function __construct ($db)
|
2016-10-13 15:07:48 +00:00
|
|
|
{
|
2017-11-29 10:01:48 +00:00
|
|
|
$this->conf = $db->getObject (
|
2016-10-13 15:07:48 +00:00
|
|
|
'SELECT host, port, secure, sender, sender_name, user, password
|
2017-11-29 10:01:48 +00:00
|
|
|
FROM hedera.mail_config'
|
2016-10-13 15:07:48 +00:00
|
|
|
);
|
2017-11-29 10:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createObject ($mailTo, $body, $subject)
|
|
|
|
{
|
|
|
|
$conf = $this->conf;
|
2016-10-13 15:07:48 +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
|
|
|
|
2017-11-29 10:01:48 +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;
|
|
|
|
$mail->Password = base64_decode ($conf->password);
|
2016-10-13 15:07:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
$mail->SMTPAuth = FALSE;
|
|
|
|
|
2017-11-29 10:01:48 +00:00
|
|
|
if ($conf->secure)
|
2016-10-13 15:07:48 +00:00
|
|
|
{
|
|
|
|
$mail->SMTPSecure = 'ssl';
|
|
|
|
$mail->Port = 465;
|
|
|
|
}
|
|
|
|
|
2017-11-29 10:01:48 +00:00
|
|
|
$mail->setFrom ($conf->sender, $conf->sender_name);
|
|
|
|
$mail->IsHTML (TRUE);
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
$mail->Body = $body;
|
|
|
|
$mail->CharSet = 'UTF-8';
|
2017-04-24 13:50:05 +00:00
|
|
|
|
|
|
|
$mailList = explode (',', $mailTo);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function send ($mailTo, $body, $subject)
|
|
|
|
{
|
|
|
|
$mail = $this->createObject ($mailTo, $body, $subject);
|
2016-10-13 15:07:48 +00:00
|
|
|
|
|
|
|
if (!$mail->Send ())
|
|
|
|
throw new UserException ('Send error: '.$mail->ErrorInfo);
|
|
|
|
}
|
|
|
|
}
|