Debian Codegraph @

classes/Mail/MailerPhpMailer.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains an class to implement sending emails using the PHPMailer library
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Mail;
33
34 use Liuch\DmarcSrg\Exception\LogicException;
35 use Liuch\DmarcSrg\Exception\RuntimeException;
36
37 /**
38 * This class is for sending emails using the PHPMailer library.
39 */
40 class MailerPhpMailer extends Mailer
41 {
42 /**
43 * Creates a message and sends it
44 *
45 * @throws LogicException
46 * @throws RuntimeException
47 *
48 * @return void
49 */
50 public function send(): void
51 {
52 $this->checkData();
53 $mailer = new \PHPMailer\PHPMailer\PHPMailer(true);
54 try {
55 $mailer->setFrom($this->from);
56 foreach ($this->addr as $to) {
57 $mailer->addAddress($to);
58 }
59 $mailer->Subject = $this->subj;
60 $mailer->XMailer = null;
61 if (is_null($this->body->html)) {
62 $mailer->Body = implode("\r\n", $this->body->text);
63 } else {
64 $mailer->isHTML();
65 $mailer->Body = implode("\r\n", $this->body->html);
66 if (!is_null($this->body->text)) {
67 $mailer->AltBody = implode("\r\n", $this->body->text);
68 }
69 }
70 $method = $this->params['method'] ?? 'mail';
71 if ($method === 'smtp') {
72 $mailer->isSMTP();
73 $mailer->Host = $this->params['host'];
74 $mailer->Port = $this->params['port'];
75 switch ($this->params['encryption'] ?? 'ssl') {
76 case 'ssl':
77 $mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS;
78 break;
79 case 'starttls':
80 $mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
81 break;
82 case 'none':
83 $mailer->SMTPAutoTLS = false;
84 break;
85 default:
86 $mailer->SMTPAutoTLS = true;
87 break;
88 }
89 $username = $this->params['username'] ?? null;
90 if (!empty($username)) {
91 $mailer->SMTPAuth = true;
92 $mailer->Username = $username;
93 $mailer->Password = $this->params['password'] ?? '';
94 }
95 //$mailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER; // for testing purposes
96 } elseif ($method === 'mail') {
97 $mailer->isMail();
98 } else {
99 throw new LogicException('Unsupported sending method');
100 }
101 if ($this->params['novalidate-cert'] ?? false) {
102 $mailer->SMTPOptions = [
103 'ssl' => [
104 'verify_peer' => false,
105 'verify_peer_name' => false,
106 'allow_self_signed' => true
107 ]
108 ];
109 }
110 $mailer->send();
111 } catch (\Exception $e) {
112 throw new RuntimeException($e->getMessage());
113 }
114 }
115
116 /**
117 * Checks if the minimum required data is available to send the email
118 *
119 * @throws LogicException
120 *
121 * @return void
122 */
123 protected function checkData(): void
124 {
125 parent::checkData();
126 if (isset($this->params['method']) && $this->params['method'] === 'smtp') {
127 if (empty($this->params['host'])) {
128 throw new LogicException('The SMTP server host is not specified');
129 }
130 if (!is_int($this->params['port']) || $this->params['port'] <= 0) {
131 throw new LogicException('The SMTP server port is not specified');
132 }
133 }
134 }
135 }