forked from rmrevin/yii2-postman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Component.php
144 lines (120 loc) · 3.58 KB
/
Component.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Component.php
* @author Roman Revin http://phptime.ru
*/
namespace rmrevin\yii\postman;
use PHPMailer;
/**
* Class Component
* The main class to wrap a config of PHPMailer
* @package rmrevin\yii\postman
*/
class Component extends \yii\base\Component
{
const COMPONENT = 'postman';
/** @var array the default value for the "From" field */
public $default_from = ['mailer@localhost', 'Mailer'];
/** @var string|null the string that is added to the beginning of the letter subject */
public $subject_prefix = null;
/** @var string|null the string that is added to the end of the letter subject */
public $subject_suffix = null;
/** @var string a name of the db table for letters */
public $table = '{{%postman_letter}}';
/** @var string a path to views of letters */
public $view_path = '/email';
/** @var string a driver for sending mail [mail|qmail|sendmail|smtp] */
public $driver = 'mail';
/** @var array smtp config */
public $smtp_config = [
'host' => 'localhost',
'port' => 25,
'auth' => false,
'user' => '',
'password' => '',
'secure' => false, // Sets connection prefix. Options are "", "ssl" or "tls"
'debug' => false,
];
/** @var PHPMailer object */
private $_mailer = null;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$mailer = new PHPMailer();
$mailer->CharSet = 'utf-8';
$this->_mailer = $mailer;
$this->setDefaultFrom($this->default_from);
$this->reconfigureDriver();
}
/**
* the method adjusts the selected driver to send emails
* @return self
* @throws \rmrevin\yii\postman\Exception
*/
public function reconfigureDriver()
{
$mailer = $this->_mailer;
switch ($this->driver) {
case 'mail':
$mailer->IsMail();
break;
case 'qmail':
$mailer->IsQmail();
break;
case 'sendmail':
$mailer->IsSendmail();
break;
case 'smtp':
$mailer->IsSMTP();
$mailer->Host = $this->smtp_config['host'];
$mailer->Port = $this->smtp_config['port'];
$mailer->SMTPAuth = $this->smtp_config['auth'];
$mailer->Username = $this->smtp_config['user'];
$mailer->Password = $this->smtp_config['password'];
$mailer->SMTPSecure = $this->smtp_config['secure'];
$mailer->SMTPDebug = $this->smtp_config['debug'];
$mailer->SMTPOptions['ssl'] = $this->smtp_config['ssl'];
break;
default:
throw new Exception(\Yii::t('app', 'Could not determine the driver is sending letters.'));
}
return $this;
}
/**
* @param array $from
* @return $this
*/
public function setDefaultFrom($from)
{
$this->default_from = $from;
$this->_mailer->SetFrom($from[0], $from[1]);
return $this;
}
/**
* @return PHPMailer
*/
public function getMailerObject()
{
return $this->_mailer;
}
/**
* factory method to create clones of "Postman"
* @return PHPMailer
*/
public function getCloneMailerObject()
{
return clone $this->_mailer;
}
/**
* @static
* @return null|self
* @throws \yii\base\InvalidConfigException
*/
public static function get()
{
return \Yii::$app->get(static::COMPONENT);
}
}