hedera-web/web/mailer.php

59 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace Vn\Web;
require_once 'libphp-phpmailer/autoload.php';
use Vn\Lib\UserException;
2018-05-23 10:14:20 +00:00
class Mailer {
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'
);
}
2018-05-23 10:14:20 +00:00
function createObject($mailTo, $body, $subject) {
$conf = $this->conf;
2020-01-23 13:05:58 +00:00
$mail = new \PHPMailer();
2018-05-23 10:14:20 +00:00
$mail->isSMTP();
$mail->Host = $conf->host;
2018-05-23 10:14:20 +00:00
if (!empty($conf->user)) {
$mail->SMTPAuth = TRUE;
$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
$mail->SMTPAuth = FALSE;
2018-05-23 10:14:20 +00:00
if ($conf->secure) {
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
}
2018-05-23 10:14:20 +00:00
$mail->setFrom($conf->sender, $conf->senderName);
$mail->IsHTML(TRUE);
$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
foreach ($mailList as $to)
2018-05-23 10:14:20 +00:00
$mail->AddAddress($to);
return $mail;
}
2018-05-23 10:14:20 +00:00
function send($mailTo, $body, $subject) {
$mail = $this->createObject($mailTo, $body, $subject);
2018-05-23 10:14:20 +00:00
if (!$mail->Send())
throw new UserException('Send error: '.$mail->ErrorInfo);
}
}