-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMTPAccount.php
262 lines (255 loc) · 7.25 KB
/
SMTPAccount.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace webfiori\email;
/**
* A class that represents SMTP account which is used to connect to SMTP server.
*
* @author Ibrahim
*
* @version 1.0.3
*/
class SMTPAccount {
/**
* The name of SMTP account.
*
* @var string
*
*/
private $accName;
/**
* Email address.
*
* @var string
*/
private $address;
/**
* Server address of the email account.
*
* @var string
*
*/
private $emailServerAddress;
/**
* The name of the email account.
*
* @var string
*
*/
private $name;
/**
* The password of the user account.
*
* @var string
*
*/
private $password;
/**
* The port number that is used to access the email server.
*
* @var int
*
*/
private $port;
/**
* The username that is used to log-in.
*
* @var string
*
*/
private $userName;
/**
* Creates new instance of the class.
*
* @param array $options An optional array that contains connection info. The array
* can have the following indices:
* <ul>
* <li><b>port</b>: SMTP server port address. usually 25 or 465.</li>
* <li><b>server-address</b>: SMTP server address.</li>
* <li><b>user</b>: The username at which it is used to log in to SMTP server.</li>
* <li><b>pass</b>: The password of the user</li>
* <li><b>sender-name</b>: The name of the sender that will appear when the
* message is sent.</li>
* <li><b>sender-address</b>: The address that will appear when the
* message is sent. Usually, it is the same as the username.</li>
* <li><b>account-name</b>: A unique name for the account. Used when creating
* new email message. If not provided, 'sender-name' is used.</li>
* </ul>
*
*/
public function __construct(array $options = []) {
if (isset($options[AccountOption::PORT])) {
$this->setPort($options[AccountOption::PORT]);
} else {
$this->setPort(465);
}
if (isset($options[AccountOption::USERNAME])) {
$this->setUsername($options[AccountOption::USERNAME]);
} else {
$this->setUsername('');
}
if (isset($options[AccountOption::PASSWORD])) {
$this->setPassword($options[AccountOption::PASSWORD]);
} else {
$this->setPassword('');
}
if (isset($options[AccountOption::SERVER_ADDRESS])) {
$this->setServerAddress($options[AccountOption::SERVER_ADDRESS]);
} else {
$this->setServerAddress('');
}
if (isset($options[AccountOption::SENDER_NAME])) {
$this->setSenderName($options[AccountOption::SENDER_NAME]);
} else {
$this->setSenderName('');
}
if (isset($options[AccountOption::SENDER_ADDRESS])) {
$this->setAddress($options[AccountOption::SENDER_ADDRESS]);
} else {
$this->setAddress('');
}
if (isset($options[AccountOption::NAME])) {
$this->setAccountName($options[AccountOption::NAME]);
} else {
$this->setAccountName($this->getSenderName());
}
}
/**
* Returns the name of the account.
*
* The name of the account is used by the class 'EmailMessage' when creating
* new instance of the class. Also, the name is used when storing account
* information.
*
* @return string A string that represents the name of the account.
*/
public function getAccountName() : string {
return $this->accName;
}
/**
* Returns the email address.
*
* @return string The email address which will be used in the header
* 'FROM' when sending an email. Default is empty string.
*
*/
public function getAddress() : string {
return $this->address;
}
/**
* Returns the password of the account that will be used to access SMTP server.
*
* @return string The password of the user account that is used to access email server.
* default is empty string.
*
*/
public function getPassword() : string {
return $this->password;
}
/**
* Returns SMTP server port number.
*
* @return int Default is 465.
*
*/
public function getPort() : int {
return $this->port;
}
/**
* Returns the name of sender that will be used in the 'FROM' header.
*
* @return string The name of the email sender. Usually this is similar to
* email address but can also be a name.
*
*/
public function getSenderName() : string {
return $this->name;
}
/**
* Returns SMTP server address.
*
* @return string The address of the SMTP server (such as 'mail.example.com').
*
*/
public function getServerAddress() : string {
return $this->emailServerAddress;
}
/**
* Returns the username that is used to access SMTP server.
*
* @return string The username that is used to access email server. Default
* is empty string.
*
*/
public function getUsername() : string {
return $this->userName;
}
/**
* Sets the name of the account.
*
* The name of the account is used by the class 'EmailMessage' when creating
* new instance of the class. Also, the name is used when storing the account.
*
* @param string $name The name of the account.
*
*/
public function setAccountName(string $name) {
$this->accName = $name;
}
/**
* Sets the email address.
*
* @param string $address An email address.
*
*/
public function setAddress(string $address) {
$this->address = trim($address);
}
/**
* Sets the password of the user account that is used to access email server.
*
* @param string $pass The password of the user account that is used to access email server.
*
*/
public function setPassword(string $pass) {
$this->password = $pass;
}
/**
* Sets port number of SMTP server.
*
* @param int $port The port number of email server. Common ports are 25, 465
* and 586.
*
*/
public function setPort(int $port) {
$this->port = $port;
}
/**
* Sets the name of the email account.
*
* @param string $name The name of the account (such as 'Programming Team').
* The name is used when sending an email message using the given SMTP account.
* The name will be used in the header
* 'FROM' when sending an email.
*
*/
public function setSenderName(string $name) {
$this->name = trim($name);
}
/**
* Sets the address of the email server.
*
* @param string $addr The address of the email server (such as 'mail.example.com').
*
*/
public function setServerAddress(string $addr) {
$this->emailServerAddress = trim($addr);
}
/**
* Sets the username that is used to access email server.
*
* @param string $u The username that is used to access email server.
*
*/
public function setUsername(string $u) {
$this->userName = trim($u);
}
}