forked from hamidsamak/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 5
/
classSimpleMail.php
executable file
·184 lines (148 loc) · 3.97 KB
/
classSimpleMail.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
<?php
/**
* PHP Simple Mail (SMTP)
* @Original author Hamid Samak <hamid@limny.org>
* @Original code at https://github.com/hamidsamak/simplemail
* @Additional work by halojoy https://github.com/halojoy
* @Code at https://github.com/halojoy/SimpleMail
* @Copyright 2016 Hamid Samak
* @License MIT License
*/
class SimpleMail {
// smtp host
public $host;
// smtp port
public $port;
// security mode (ssl or tls)
public $security;
// smtp username
public $user;
// smtp password
public $pass;
// mail from
public $from;
// to recipient
public $to = array();
// mail subject
public $subject;
// message content
public $message;
// mail content type (text/html or text/plain)
public $type = 'text/html';
// mail encoding
public $encoding = 'UTF-8';
// error message
public $error;
// print results
public $debug = false;
/////////////////////////////////////////////////////////////////
public function __construct($host, $port, $security = 'ssl')
{
$this->host = $host;
$this->port = $port;
$this->security = $security;
}
public function auth($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
}
/**
* 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 recipient(s)
* @param string $address email address
* @param string $name recipient 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;
$socket = fsockopen($host, $this->port, $errno, $errstr);
if ($socket === false) {
$this->error = $errno . ' ' . $errstr;
return false;
} else if ($this->parse_result($socket, 220) === false)
return false;
$commands = array(
'EHLO ' . $this->host => 250
);
if ($this->security == 'tls')
$commands = array_merge($commands, array(
'STARTTLS' => 220,
'EHLO ' . $this->host => 250
));
$commands = array_merge($commands, array(
'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, array(
'DATA' => 354,
'Subject: ' . $this->subject . "\r\n" .
'To: ' . implode(', ', $this->to) . "\r\n" .
'From: ' . $this->from . "\r\n" .
'Reply-To: ' . $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;
}
}
?>