Debian Codegraph @

classes/Mail/Mailer.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 abstract wrapper class to implement sending emails
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
36 /**
37 * A wrapper class to support different methods of sending emails
38 */
39 abstract class Mailer
40 {
41 protected $params = null;
42 protected $from = null;
43 protected $addr = [];
44 protected $subj = null;
45 protected $body = null;
46
47 private function __construct(array $params)
48 {
49 $this->params = $params;
50 }
51
52 /**
53 * Factory method
54 *
55 * @param array $params Mailer parameters from the configuration file
56 *
57 * @throws LogicException
58 *
59 * @return object Instance of a class inherited from Mailer
60 */
61 public static function get(array $params)
62 {
63 $library = $params['library'] ?? 'internal';
64 switch ($library) {
65 case 'internal':
66 return new MailerInternal($params);
67 case 'phpmailer':
68 return new MailerPhpMailer($params);
69 }
70 throw new LogicException('Unsupported mailing library: ' . $library);
71 }
72
73 /**
74 * Add a "To" address
75 *
76 * @param string $address The email address to send to
77 *
78 * @return $this
79 */
80 public function addAddress(string $address)
81 {
82 if (!in_array($address, $this->addr)) {
83 $this->addr[] = $address;
84 }
85 return $this;
86 }
87
88 /**
89 * Sets the "From" email address for the message
90 *
91 * @param string $from The "From" email address
92 *
93 * @throws LogicException
94 *
95 * @return $this
96 */
97 public function setFrom(string $from)
98 {
99 if (!filter_var($from, FILTER_VALIDATE_EMAIL)) {
100 throw new LogicException('Invalid email address (From): ' . $from);
101 }
102 $this->from = $from;
103 return $this;
104 }
105
106 /**
107 * Sets the subject of the message
108 *
109 * @param string $subject The subject of the message
110 *
111 * @return $this
112 */
113 public function setSubject(string $subject)
114 {
115 $this->subj = $subject;
116 return $this;
117 }
118
119 /**
120 * Sets the body of the message
121 *
122 * @param MailBody $body The message body
123 *
124 * @return $this
125 */
126 public function setBody($body)
127 {
128 $this->body = $body;
129 return $this;
130 }
131
132 /**
133 * Creates a message and sends it
134 *
135 * @return void
136 */
137 abstract public function send(): void;
138
139 /**
140 * Checks if the minimum required data is available to send the email
141 *
142 * @throws LogicException
143 *
144 * @return void
145 */
146 protected function checkData(): void
147 {
148 if (!$this->from) {
149 throw new LogicException('The "From" address is not specified');
150 }
151 if (!count($this->addr)) {
152 throw new LogicException('The "To" address is not specified');
153 }
154 if (empty($this->subj)) {
155 throw new LogicException('The subject of the message is not specified');
156 }
157 if (!$this->body) {
158 throw new LogicException('The body of the message is not specified');
159 }
160 }
161 }