1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nip\View; |
6
|
|
|
|
7
|
|
|
use Nip\Request; |
8
|
|
|
use Nip\View; |
|
|
|
|
9
|
|
|
use Nip_Mailer; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class Email extends View |
12
|
|
|
{ |
13
|
|
|
protected $_layout = '/layouts/email'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Nip_Mailer|null |
17
|
|
|
*/ |
18
|
|
|
protected $_mail = null; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->initMailer(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function initMailer() |
26
|
|
|
{ |
27
|
|
|
$this->_mail = new Nip_Mailer(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function initBasePath() |
31
|
|
|
{ |
32
|
|
|
$this->setBasePath(MODULES_PATH . Request::instance()->getModuleName() . '/views/'); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $host |
37
|
|
|
* @param string $username |
38
|
|
|
* @param string $password |
39
|
|
|
* |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function authSMTP($host, $username, $password) |
43
|
|
|
{ |
44
|
|
|
$this->_mail->authSMTP($host, $username, $password); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Sets flag to show SMTP debugging information. |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function debugSMTP() |
55
|
|
|
{ |
56
|
|
|
$this->_mail->debugSMTP(); |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $address |
63
|
|
|
* @param string|bool $name |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setFrom($address, $name = false) |
68
|
|
|
{ |
69
|
|
|
$this->_mail->setFrom($address, $name); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $address |
76
|
|
|
* @param string|bool $name |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function addTo($address, $name = false) |
81
|
|
|
{ |
82
|
|
|
$this->_mail->addTo($address, $name); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $address |
89
|
|
|
* @param string $name |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function addBCC($address, $name = '') |
94
|
|
|
{ |
95
|
|
|
$this->_mail->addBCC($address, $name); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $address |
102
|
|
|
* @param string|bool $name |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function addReplyTo($address, $name = false) |
107
|
|
|
{ |
108
|
|
|
$this->_mail->addReplyTo($address, $name); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function clearAllRecipients() |
117
|
|
|
{ |
118
|
|
|
$this->_mail->clearAllRecipients(); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $subject |
125
|
|
|
* |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setSubject($subject) |
129
|
|
|
{ |
130
|
|
|
$this->_mail->setSubject($subject); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds attachment. |
137
|
|
|
* |
138
|
|
|
* @param string $path |
139
|
|
|
* @param string $name |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function addAttachment($path, $name = '') |
144
|
|
|
{ |
145
|
|
|
$this->_mail->addAttachment($path, $name); |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function send() |
151
|
|
|
{ |
152
|
|
|
if (!$this->getBody()) { |
153
|
|
|
$this->setBody($this->load($this->_layout, [], true)); |
154
|
|
|
} |
155
|
|
|
$this->_mail->setAltBody($this->getBody()); |
156
|
|
|
|
157
|
|
|
return $this->_mail->send(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setLayout($layout) |
161
|
|
|
{ |
162
|
|
|
$this->_layout = $layout; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function setBody($body) |
166
|
|
|
{ |
167
|
|
|
$this->_mail->setBody($body); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getBody() |
173
|
|
|
{ |
174
|
|
|
return $this->_mail->getBody(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: