1: <?php
2:
3: namespace Charcoal\Email;
4:
5: // From `PHP`
6: use \InvalidArgumentException as InvalidArgumentException;
7:
8: // From `charcoal-core`
9: use \Charcoal\Config\AbstractConfig;
10:
11: /**
12: * Email configuration.
13: */
14: class EmailConfig extends AbstractConfig
15: {
16: use EmailAwareTrait;
17:
18: /**
19: * Whether SMTP should be used.
20: *
21: * @var boolean $smtp
22: */
23: private $smtp = false;
24:
25: /**
26: * The SMTP hostname.
27: *
28: * @var string $smtpHostname
29: */
30: private $smtpHostname;
31:
32: /**
33: * The SMTP port.
34: *
35: * @var integer $smtpPort
36: */
37: private $smtpPort;
38:
39: /**
40: * The SMTP security type.
41: *
42: * @var string $smtpSecurity
43: */
44: private $smtpSecurity = '';
45:
46: /**
47: * Whether SMTP requires authentication.
48: *
49: * @var boolean $smtpAuth
50: */
51: private $smtpAuth;
52:
53: /**
54: * The SMTP username.
55: *
56: * @var string $smtpUsername
57: */
58: private $smtpUsername;
59:
60: /**
61: * The SMTP password.
62: *
63: * @var string $smtpPassword
64: */
65: private $smtpPassword;
66:
67: /**
68: * The default sender's email address.
69: *
70: * @var string $defaultFrom
71: */
72: private $defaultFrom;
73:
74: /**
75: * The default "Reply-To" email address.
76: *
77: * @var string $defaultReplyTo
78: */
79: private $defaultReplyTo;
80:
81: /**
82: * Whether the email should be tracked by default.
83: *
84: * @var boolean $defaultTrack
85: */
86: private $defaultTrack;
87:
88: /**
89: * Whether the email should be logged by default.
90: *
91: * @var boolean $defaultLog
92: */
93: private $defaultLog;
94:
95: /**
96: * Default email configuration.
97: *
98: * @return array
99: */
100: public function defaults()
101: {
102: return [
103: 'smtp' => false,
104:
105: 'default_from' => '',
106: 'default_reply_to' => '',
107:
108: 'default_track' => false,
109: 'default_log' => true
110: ];
111: }
112:
113: /**
114: * Set whether SMTP should be used for sending the email.
115: *
116: * @param boolean $smtp If the email should be sent using SMTP or not.
117: * @throws InvalidArgumentException If the SMTP state is not a boolean.
118: * @return EmailConfig Chainable
119: */
120: public function setSmtp($smtp)
121: {
122: $this->smtp = !!$smtp;
123: return $this;
124: }
125:
126: /**
127: * Determine if SMTP should be used.
128: *
129: * @return boolean
130: */
131: public function smtp()
132: {
133: return $this->smtp;
134: }
135:
136: /**
137: * Set the SMTP hostname to be used.
138: *
139: * @param string $hostname The SMTP hostname.
140: * @throws InvalidArgumentException If the SMTP hostname is not a string.
141: * @return EmailConfig Chainable
142: */
143: public function setSmtpHostname($hostname)
144: {
145: if (!is_string($hostname)) {
146: throw new InvalidArgumentException(
147: 'SMTP Hostname must be a string.'
148: );
149: }
150:
151: $this->smtpHostname = $hostname;
152:
153: return $this;
154: }
155:
156: /**
157: * Get the SMTP hostname.
158: *
159: * @return string
160: */
161: public function smtpHostname()
162: {
163: return $this->smtpHostname;
164: }
165:
166: /**
167: * Set the SMTP port to be used.
168: *
169: * @param integer $port The SMTP port.
170: * @throws InvalidArgumentException If the SMTP port is not an integer.
171: * @return EmailConfig Chainable
172: */
173: public function setSmtpPort($port)
174: {
175: if (!is_int($port)) {
176: throw new InvalidArgumentException(
177: 'SMTP Port must be an integer.'
178: );
179: }
180:
181: $this->smtpPort = $port;
182:
183: return $this;
184: }
185:
186: /**
187: * Get the SMTP port.
188: *
189: * @return integer
190: */
191: public function smtpPort()
192: {
193: return $this->smtpPort;
194: }
195:
196: /**
197: * Set whether SMTP requires authentication.
198: *
199: * @param boolean $auth The SMTP authentication flag (if auth is required).
200: * @return EmailConfig Chainable
201: */
202: public function setSmtpAuth($auth)
203: {
204: $this->smtpAuth = !!$auth;
205: return $this;
206: }
207:
208: /**
209: * Determine if SMTP requires authentication.
210: *
211: * @return boolean
212: */
213: public function smtpAuth()
214: {
215: return $this->smtpAuth;
216: }
217:
218: /**
219: * Set the SMTP username to be used.
220: *
221: * @param string $username The SMTP username, if using authentication.
222: * @throws InvalidArgumentException If the SMTP username is not a string.
223: * @return EmailConfig Chainable
224: */
225: public function setSmtpUsername($username)
226: {
227: if (!is_string($username)) {
228: throw new InvalidArgumentException(
229: 'SMTP Username must be a string.'
230: );
231: }
232:
233: $this->smtpUsername = $username;
234:
235: return $this;
236: }
237:
238: /**
239: * Get the SMTP username.
240: *
241: * @return string
242: */
243: public function smtpUsername()
244: {
245: return $this->smtpUsername;
246: }
247:
248: /**
249: * Set the SMTP password to be used.
250: *
251: * @param string $password The SMTP password, if using authentication.
252: * @throws InvalidArgumentException If the SMTP password is not a string.
253: * @return EmailConfig Chainable
254: */
255: public function setSmtpPassword($password)
256: {
257: if (!is_string($password)) {
258: throw new InvalidArgumentException(
259: 'SMTP Password must be a string.'
260: );
261: }
262:
263: $this->smtpPassword = $password;
264:
265: return $this;
266: }
267:
268: /**
269: * Get the SMTP password.
270: *
271: * @return string
272: */
273: public function smtpPassword()
274: {
275: return $this->smtpPassword;
276: }
277:
278: /**
279: * Set the SMTP security type to be used.
280: *
281: * @param string $security The SMTP security type (empty, "TLS", or "SSL").
282: * @throws InvalidArgumentException If the security type is not valid (empty, "TLS", or "SSL").
283: * @return EmailConfig Chainable
284: */
285: public function setSmtpSecurity($security)
286: {
287: $security = strtoupper($security);
288: $validSecurity = [ '', 'TLS', 'SSL' ];
289:
290: if (!in_array($security, $validSecurity)) {
291: throw new InvalidArgumentException(
292: 'SMTP Security is not valid. Must be "", "TLS" or "SSL".'
293: );
294: }
295:
296: $this->smtpSecurity = $security;
297:
298: return $this;
299: }
300:
301: /**
302: * Get the SMTP security type.
303: *
304: * @return string
305: */
306: public function smtpSecurity()
307: {
308: return $this->smtpSecurity;
309: }
310:
311: /**
312: * Set the default sender's email address.
313: *
314: * @param string|array $email The default "From" email address.
315: * @throws InvalidArgumentException If the email address is invalid.
316: * @return EmailConfig Chainable
317: */
318: public function setDefaultFrom($email)
319: {
320: if (is_string($email)) {
321: $this->defaultFrom = $email;
322: } elseif (is_array($email)) {
323: $this->defaultFrom = $this->emailFromArray($email);
324: } else {
325: throw new InvalidArgumentException(
326: 'Default sender email address must be an array or a string.'
327: );
328: }
329:
330: return $this;
331: }
332:
333: /**
334: * Get the sender email address.
335: *
336: * @return string
337: */
338: public function defaultFrom()
339: {
340: return $this->defaultFrom;
341: }
342:
343: /**
344: * Set the default "Reply-To" email address.
345: *
346: * @param string|array $email The default "Reply-To" email address.
347: * @throws InvalidArgumentException If the email address is invalid.
348: * @return EmailConfig Chainable
349: */
350: public function setDefaultReplyTo($email)
351: {
352: if (is_string($email)) {
353: $this->defaultReplyTo = $email;
354: } elseif (is_array($email)) {
355: $this->defaultReplyTo = $this->emailFromArray($email);
356: } else {
357: throw new InvalidArgumentException(
358: 'Default reply-to email address must be an array or a string.'
359: );
360: }
361:
362: return $this;
363: }
364:
365: /**
366: * Get the "Reply-To" email address.
367: *
368: * @return string
369: */
370: public function defaultReplyTo()
371: {
372: return $this->defaultReplyTo;
373: }
374:
375: /**
376: * Set whether the email should be logged by defaultd.
377: *
378: * @param boolean $log The default log flag.
379: * @return EmailConfig Chainable
380: */
381: public function setDefaultLog($log)
382: {
383: $this->defaultLog = !!$log;
384: return $this;
385: }
386:
387: /**
388: * Determine if the email should be logged by default.
389: *
390: * @return boolean
391: */
392: public function defaultLog()
393: {
394: return $this->defaultLog;
395: }
396:
397: /**
398: * Set whether the email should be tracked by default.
399: *
400: * @param boolean $track The default track flag.
401: * @return EmailConfig Chainable
402: */
403: public function setDefaultTrack($track)
404: {
405: $this->defaultTrack = !!$track;
406: return $this;
407: }
408:
409: /**
410: * Determine if the email should be tracked by default.
411: *
412: * @return boolean
413: */
414: public function defaultTrack()
415: {
416: return $this->defaultTrack;
417: }
418: }
419: