Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | <?php |
| 2 | use PHPMailer\PHPMailer\PHPMailer; |
| 3 | use PHPMailer\PHPMailer\Exception; |
| 4 | use PHPMailer\PHPMailer\SMTP; |
| 5 | |
| 6 | require __DIR__.'/../lib/PHPMailer/Exception.php'; |
| 7 | require __DIR__.'/../lib/PHPMailer/PHPMailer.php'; |
| 8 | require __DIR__.'/../lib/PHPMailer/SMTP.php'; |
| 9 | |
| 10 | class mail { |
| 11 | public static function send($to, $attachments, $subject, $body, $isHTML = true, $plainBody = "") { |
| 12 | global $conf; |
| 13 | |
| 14 | if (!$conf["mail"]["enabled"]) return false; |
| 15 | |
| 16 | $mail = new PHPMailer(); |
| 17 | |
| 18 | try { |
| 19 | if ($conf["debug"]) { |
| 20 | $mail->SMTPDebug = SMTP::DEBUG_SERVER; |
| 21 | } |
| 22 | $mail->CharSet = "UTF-8"; |
| 23 | $mail->isSMTP(); |
| 24 | $mail->Host = $conf["mail"]["host"]; |
| 25 | $mail->SMTPAuth = $conf["mail"]["smtpauth"]; |
| 26 | $mail->Username = $conf["mail"]["username"]; |
| 27 | $mail->Password = $conf["mail"]["password"]; |
| 28 | $mail->SMTPSecure = 'tls'; |
| 29 | $mail->Port = $conf["mail"]["port"]; |
| 30 | $mail->Encoding = 'base64'; |
| 31 | $mail->Timeout = 30; |
| 32 | |
| 33 | $mail->setFrom($conf["mail"]["remitent"], $conf["mail"]["remitentName"]); |
| 34 | |
| 35 | foreach ($to as $address) { |
| 36 | if (isset($address["name"])) $mail->addAddress($address["email"], $address["name"]); |
| 37 | else $mail->addAddress($address["email"]); |
| 38 | } |
| 39 | |
| 40 | foreach ($attachments as $attachment) { |
| 41 | if (isset($attachment["name"])) $mail->addAttachment($attachment["path"], $attachment["name"]); |
| 42 | else $mail->addAttachment($attachment["email"]); |
| 43 | } |
| 44 | |
| 45 | if ($isHTML) { |
| 46 | $mail->isHTML(true); |
| 47 | if (!empty($plainBody)) $mail->AltBody = $plainBody; |
| 48 | } |
| 49 | |
| 50 | $mail->Subject = (!empty($conf["mail"]["subjectPrefix"]) ? $conf["mail"]["subjectPrefix"]." " : "").$subject; |
| 51 | $mail->Body = $body; |
| 52 | |
| 53 | if (!$mail->send()) return false; |
| 54 | } catch (Exception $e) { |
| 55 | if ($conf["debug"]) echo $e."\n"; |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | public static function bodyTemplate($msg) { |
| 63 | return "<div style=\"font-family: 'Helvetica', 'Arial', sans-serif;\">".$msg."</div>"; |
| 64 | } |
| 65 | } |