|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fabrica\Helper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class MailerFactory helps to set up and configure a SwiftMailer object. |
|
7
|
|
|
*/ |
|
8
|
|
|
class MailerFactory |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $emailConfig; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Set the mailer factory configuration. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $config |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($config = []) |
|
21
|
|
|
{ |
|
22
|
|
|
if (!is_array($config)) { |
|
23
|
|
|
$config = []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->emailConfig = isset($config['email_settings']) ? $config['email_settings'] : []; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Returns an instance of Swift_Mailer based on the config.s |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Swift_Mailer |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getSwiftMailerFromConfig() |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->getMailConfig('smtp_address')) { |
|
|
|
|
|
|
37
|
|
|
$encryptionType = $this->getMailConfig('smtp_encryption'); |
|
38
|
|
|
|
|
39
|
|
|
// Workaround issue where smtp_encryption could == 1 in the past by |
|
40
|
|
|
// checking it is a valid transport |
|
41
|
|
|
if ($encryptionType && !in_array($encryptionType, stream_get_transports())) { |
|
42
|
|
|
$encryptionType = null; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var \Swift_SmtpTransport $transport |
|
47
|
|
|
*/ |
|
48
|
|
|
$transport = \Swift_SmtpTransport::newInstance( |
|
|
|
|
|
|
49
|
|
|
$this->getMailConfig('smtp_address'), |
|
50
|
|
|
$this->getMailConfig('smtp_port'), |
|
51
|
|
|
$encryptionType |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$transport->setUsername($this->getMailConfig('smtp_username')); |
|
55
|
|
|
$transport->setPassword($this->getMailConfig('smtp_password')); |
|
56
|
|
|
} else { |
|
57
|
|
|
$transport = \Swift_MailTransport::newInstance(null); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return \Swift_Mailer::newInstance($transport); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return a specific configuration value by key. |
|
65
|
|
|
* |
|
66
|
|
|
* @param $configName |
|
67
|
|
|
* @return null|string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getMailConfig($configName) |
|
70
|
|
|
{ |
|
71
|
|
|
if (isset($this->emailConfig[$configName]) && $this->emailConfig[$configName] != "") { |
|
72
|
|
|
return $this->emailConfig[$configName]; |
|
73
|
|
|
} else { |
|
74
|
|
|
// Check defaults |
|
75
|
|
|
|
|
76
|
|
|
switch ($configName) { |
|
77
|
|
|
case 'smtp_address': |
|
78
|
|
|
return ""; |
|
79
|
|
|
case 'default_mailto_address': |
|
80
|
|
|
return null; |
|
81
|
|
|
case 'smtp_port': |
|
82
|
|
|
return '25'; |
|
83
|
|
|
case 'smtp_encryption': |
|
84
|
|
|
return null; |
|
85
|
|
|
default: |
|
86
|
|
|
return ""; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: