blob: e767d0f8c75a812213863bece0daf8e02ee8bdca [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
3 * hores
4 * Copyright (c) 2023 Adrià Vilanova Martínez
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program.
18 * If not, see http://www.gnu.org/licenses/.
19 */
20
Copybara botbe50d492023-11-30 00:16:42 +010021use PHPMailer\PHPMailer\PHPMailer;
22use PHPMailer\PHPMailer\Exception;
23use PHPMailer\PHPMailer\SMTP;
24
25require __DIR__.'/../lib/PHPMailer/Exception.php';
26require __DIR__.'/../lib/PHPMailer/PHPMailer.php';
27require __DIR__.'/../lib/PHPMailer/SMTP.php';
28
29class mail {
30 public static function send($to, $attachments, $subject, $body, $isHTML = true, $plainBody = "") {
31 global $conf;
32
33 if (!$conf["mail"]["enabled"]) return false;
34
35 $mail = new PHPMailer();
36
37 try {
38 if ($conf["debug"]) {
39 $mail->SMTPDebug = SMTP::DEBUG_SERVER;
40 }
41 $mail->CharSet = "UTF-8";
42 $mail->isSMTP();
43 $mail->Host = $conf["mail"]["host"];
44 $mail->SMTPAuth = $conf["mail"]["smtpauth"];
45 $mail->Username = $conf["mail"]["username"];
46 $mail->Password = $conf["mail"]["password"];
47 $mail->SMTPSecure = 'tls';
48 $mail->Port = $conf["mail"]["port"];
49 $mail->Encoding = 'base64';
50 $mail->Timeout = 30;
51
52 $mail->setFrom($conf["mail"]["remitent"], $conf["mail"]["remitentName"]);
53
54 foreach ($to as $address) {
55 if (isset($address["name"])) $mail->addAddress($address["email"], $address["name"]);
56 else $mail->addAddress($address["email"]);
57 }
58
59 foreach ($attachments as $attachment) {
60 if (isset($attachment["name"])) $mail->addAttachment($attachment["path"], $attachment["name"]);
61 else $mail->addAttachment($attachment["email"]);
62 }
63
64 if ($isHTML) {
65 $mail->isHTML(true);
66 if (!empty($plainBody)) $mail->AltBody = $plainBody;
67 }
68
69 $mail->Subject = (!empty($conf["mail"]["subjectPrefix"]) ? $conf["mail"]["subjectPrefix"]." " : "").$subject;
70 $mail->Body = $body;
71
72 if (!$mail->send()) return false;
73 } catch (Exception $e) {
74 if ($conf["debug"]) echo $e."\n";
75 return false;
76 }
77
78 return true;
79 }
80
81 public static function bodyTemplate($msg) {
82 return "<div style=\"font-family: 'Helvetica', 'Arial', sans-serif;\">".$msg."</div>";
83 }
84}