92 lines
2.1 KiB
PHP
92 lines
2.1 KiB
PHP
<?php
|
|
require_once (__DIR__. '/../3rdparty/phpmailer/phpmailer.php');
|
|
require_once (__DIR__. '/../3rdparty/phpmailer/smtp.php');
|
|
|
|
class Emailer {
|
|
|
|
public static function sendEmail($pdo,$msg,$to,$subject,$bcc = null) {
|
|
if (!is_null($bcc)) {
|
|
$bcc = trim($bcc);
|
|
}
|
|
if ($bcc == '') {
|
|
$bcc = null;
|
|
}
|
|
$mail = new PHPMailer;
|
|
|
|
|
|
$mail->CharSet = 'utf-8';
|
|
|
|
$mail->Timeout = 7;
|
|
|
|
$host = self::getConfigItemOrDefault($pdo, "smtphost", "");
|
|
if ($host == "") {
|
|
return false;
|
|
}
|
|
|
|
$mail->isSMTP(); // Set mailer to use SMTP
|
|
$mail->Host = $host;
|
|
$smtpAuth = self::getConfigItemOrDefault($pdo, "smtpauth", 1);
|
|
if ($smtpAuth == 0) {
|
|
$mail->SMTPAuth = false;
|
|
} else {
|
|
$mail->SMTPAuth = true;
|
|
}
|
|
|
|
$mail->Username = self::getConfigItemOrDefault($pdo, "smtpuser", "");
|
|
$mail->Password = self::getConfigItemOrDefault($pdo, "smtppass", "");
|
|
|
|
$smtpSecure = self::getConfigItemOrDefault($pdo, "smtpsecure", 1);
|
|
if ($smtpSecure == 0) {
|
|
$mail->SMTPSecure = 'ssl';
|
|
} else {
|
|
$mail->SMTPSecure = 'tls';
|
|
}
|
|
$mail->Port = self::getConfigItemOrDefault($pdo, "smtpport", 589);
|
|
|
|
$mail->setFrom(self::getConfigItemOrDefault($pdo, "email", ""));
|
|
$mail->addAddress($to); // Add a recipient
|
|
if (!is_null($bcc)) {
|
|
$mail->addBCC($bcc);
|
|
}
|
|
$mail->isHTML(true); // Set email format to HTML
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
$htmlMsg = str_replace("\n","<br>",$msg);
|
|
$mail->Body = $htmlMsg;
|
|
$mail->AltBody = $msg;
|
|
|
|
$isDemo = false;
|
|
|
|
if (!$isDemo) {
|
|
$ret = $mail->send();
|
|
return $ret;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private static function getConfigItemOrDefault($pdo,$item,$default) {
|
|
try {
|
|
$sql = "SELECT count(id) as number,setting FROM %config% WHERE name=?";
|
|
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
|
|
|
|
$stmt->execute(array($item));
|
|
$row = $stmt->fetchObject();
|
|
$ret = $default;
|
|
|
|
if ($row) {
|
|
if (($row->number) > 0) {
|
|
$ret = $row->setting;
|
|
} else {
|
|
$ret = $default;
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
$ret = $default;
|
|
}
|
|
return $ret;
|
|
}
|
|
}
|
|
|
|
?>
|