-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathMailerFactory.php
More file actions
88 lines (78 loc) · 2.26 KB
/
Copy pathMailerFactory.php
File metadata and controls
88 lines (78 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace PHPCensor\Helper;
use Swift_Mailer;
use Swift_SendmailTransport;
use Swift_SmtpTransport;
/**
* Class MailerFactory helps to set up and configure a SwiftMailer object.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class MailerFactory
{
/**
* @var array
*/
protected $emailConfig;
/**
* Set the mailer factory configuration.
* @param array $config
*/
public function __construct($config = [])
{
if (!\is_array($config)) {
$config = [];
}
$this->emailConfig = isset($config['email_settings']) ? $config['email_settings'] : [];
}
/**
* Returns an instance of Swift_Mailer based on the config.s
* @return Swift_Mailer
*/
public function getSwiftMailerFromConfig()
{
if ($this->getMailConfig('smtp_address')) {
$encryptionType = (string)$this->getMailConfig('smtp_encryption');
if (!$encryptionType) {
$encryptionType = null;
}
/** @var Swift_SmtpTransport $transport */
$transport = new Swift_SmtpTransport(
$this->getMailConfig('smtp_address'),
$this->getMailConfig('smtp_port'),
$encryptionType
);
$transport->setUsername($this->getMailConfig('smtp_username'));
$transport->setPassword($this->getMailConfig('smtp_password'));
} else {
$transport = new Swift_SendmailTransport();
}
return new Swift_Mailer($transport);
}
/**
* Return a specific configuration value by key.
*
*
* @return string|null
*/
public function getMailConfig($configName)
{
if (isset($this->emailConfig[$configName]) && '' !== $this->emailConfig[$configName]) {
return $this->emailConfig[$configName];
} else {
switch ($configName) {
case 'default_mailto_address':
case 'smtp_encryption':
return null;
case 'smtp_port':
return '25';
case 'smtp_address':
default:
return '';
}
}
}
}