-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimplemail.class.php
executable file
·186 lines (156 loc) · 3.78 KB
/
simplemail.class.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
<?php
/**
* PHP Simple Mail (SMTP)
* @author Hamid Samak <[email protected]>
* @copyright 2016 Hamid Samak
* @license MIT License
*/
class SimpleMail
{
// smtp host
public $host;
// smtp port
public $port;
// smtp username
public $user;
// smtp password
public $pass;
// security mode (ssl or tls)
public $security;
// timeout
public $timeout = 30;
// verify certificate
public $verify_peer = true;
public $verify_peer_name = true;
// mail subject
public $subject;
// message content
public $message;
// mail content type
public $type = 'text/html';
// mail encoding
public $encoding = 'UTF-8';
// error message
public $error;
// print results
public $debug = false;
// mail from
private $from;
// recipient(s)
private $to = [];
/**
* set sender
* @param string $address email address
* @param string $name sender name
* @return void
*/
public function from($address, $name = null)
{
if (empty($name)) {
$this->from = '<' . $address . '>';
} else {
$this->from = '"' . $name . '" <' . $address . '>';
}
}
/**
* set recipients
* @param string $address email address
* @param string $name sender name
* @return void
*/
public function to($address, $name = null)
{
if (empty($name)) {
$this->to[] = '<' . $address . '>';
} else {
$this->to[] = '"' . $name . '" <' . $address . '>';
}
}
/**
* send mail
* @return boolean
*/
public function send()
{
$host = $this->host;
if ($this->security == 'ssl') {
$host = 'ssl://' . $host;
}
$context = stream_context_create([
'ssl' => [
'verify_peer' => $this->verify_peer,
'verify_peer_name' => $this->verify_peer_name,
],
]);
$socket = stream_socket_client($host . ':' . $this->port, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context);
if ($socket === false) {
$this->error = $errno . ' ' . $errstr;
return false;
} else if ($this->parse_result($socket, 220) === false) {
return false;
}
$commands = [
'EHLO ' . $this->host => 250,
];
if ($this->security == 'tls') {
$commands = array_merge($commands, [
'STARTTLS' => 220,
'EHLO ' . $this->host => 250,
]);
}
$commands = array_merge($commands, [
'AUTH LOGIN' => 334,
base64_encode($this->user) => 334,
base64_encode($this->pass) => 235,
'MAIL FROM: ' . strstr($this->from, '<') => 250,
]);
foreach ($this->to as $to) {
$commands['RCPT TO: ' . strstr($to, '<')] = 250;
}
$commands = array_merge($commands, [
'DATA' => 354,
'Subject: =?UTF-8?B?' . base64_encode($this->subject) . "?=\r\n" .
'To: ' . implode(', ', $this->to) . "\r\n" .
'From: ' . $this->from . "\r\n" .
'Content-Type: ' . $this->type . "\r\n" .
'Content-Encoding: ' . $this->encoding . "\r\n\r\n" .
$this->message => -1,
'.' => 250,
'QUIT' => 0,
]);
foreach ($commands as $command => $code) {
fwrite($socket, $command . "\r\n");
if ($code > -1 && $this->parse_result($socket, $code) === false) {
$this->error .= ' (' . $command . ')';
return false;
}
if ($command == 'STARTTLS' && stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT) === false) {
$this->error .= 'Unable to start TLS encryption. (' . $command . ')';
return false;
}
}
fclose($socket);
return true;
}
/**
* parse request result and check result with expected code
* @param resource $socket connection
* @param integer $code expected code
* @return boolean
*/
private function parse_result($socket, $code)
{
$result = '';
while (substr($result, 3, 1) != ' ') {
$result = fgets($socket, 256);
}
if ($this->debug === true) {
echo $result . '<br>' . "\n";
}
if (empty($code) || substr($result, 0, 3) == $code) {
return true;
}
$this->error = $result;
return false;
}
}