ordersprinter/webapp/php/utilities/Emailer.php

92 lines
2.1 KiB
PHP
Raw Normal View History

2020-11-19 22:47:44 +01:00
<?php
2020-11-19 23:02:19 +01:00
require_once (__DIR__. '/../3rdparty/phpmailer/phpmailer.php');
2020-11-19 22:47:44 +01:00
require_once (__DIR__. '/../3rdparty/phpmailer/smtp.php');
2020-11-19 23:02:19 +01:00
class Emailer {
2020-11-19 22:47:44 +01:00
2020-11-19 23:02:19 +01:00
public static function sendEmail($pdo,$msg,$to,$subject,$bcc = null) {
if (!is_null($bcc)) {
$bcc = trim($bcc);
}
if ($bcc == '') {
$bcc = null;
}
2020-11-19 22:47:44 +01:00
$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
2020-11-19 23:02:19 +01:00
if (!is_null($bcc)) {
$mail->addBCC($bcc);
}
2020-11-19 22:47:44 +01:00
$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 {
2020-11-19 23:02:19 +01:00
return true;
2020-11-19 22:47:44 +01:00
}
}
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;
}
}
?>