-
Notifications
You must be signed in to change notification settings - Fork 4
/
SmtpMailer.php
193 lines (167 loc) · 4.1 KB
/
SmtpMailer.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
<?php
/**
* A mail extension for yii
*
* Based on smtp protocol
*
* @link http://github.com/tlikai/teaconf
* @author likai<youyuge@gmail.com>
* @license http://www.teaconf.com/license New BSD License
*/
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Mailer.php';
class SmtpMailer extends Mailer
{
/**
* Smtp server address
*
* @var string
*/
public $server;
/**
* Smtp server port
*
* @var integer
*/
public $port = 25;
/**
* Connecting timeout
*
* @var integer
*/
public $timeout = 3;
/**
* Say hello command
*
* @var string
*/
public $hello = 'EHLO';
/**
* auth username
*
* @var string
*/
public $username;
/**
* auth password
*
* @var string
*/
public $password;
/**
* smtp server return code
*
* @var integer
*/
public $code;
/**
* smtp server return data
*
* @var integer
*/
public $return;
/**
* fsockopen handle
*
* @var resource
*/
private $_fp;
/**
* init fsockopen connect
*/
public function init()
{
parent::init();
$this->_fp = fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
if(!$this->_fp)
throw new CException('Connect to smtp server failed: ' . $errstr);
$this->hello();
$this->authenticate();
}
/**
* send mail
*
* @param mixed $to
* @param string $subject
* @param string $message
*
* @return boolean
*/
public function send($to, $subject, $message)
{
$to = is_array($to) ? $to : array($to);
$message = str_replace("\r\n", "\n", $message);
$code = $this->put("MAIL FROM:<{$this->username}>");
if($code != 250 && $code != 235)
throw new CException($this->return);
foreach($to as $email)
{
$code = $this->put("RCPT TO:<{$email}>");
if($code != 250)
throw new CException($this->return);
}
$code = $this->put("DATA");
if($code != 334 && $code != 250)
throw new CException($this->return);
$output = '';
foreach($to as $email)
$output .= "To: {$email}{$this->crlf}";
$output .= "Date: " . gmdate('r') . $this->crlf;
$output .= "From: {$this->username}{$this->crlf}";
$output .= "Subject: {$subject}{$this->crlf}";
foreach($this->headers as $header)
$output .= $header . $this->crlf;
$output .= $this->crlf . $this->crlf;
$output .= $message;
$output .= $this->crlf . ".";
$code = $this->put($output);
fclose($this->_fp);
if($code != 250)
return false;
return true;
}
/**
* say hello
*/
protected function hello()
{
$auth = strtoupper($this->hello) == 'EHLO' ? 'EHLO' : 'HELO';
$code = $this->put("{$auth} {$_SERVER['HTTP_HOST']}");
if($code != 220)
throw new CException($this->return);
}
/**
* authenticate
*/
protected function authenticate()
{
$code = $this->put("AUTH LOGIN");
if($code != 250)
throw new CException($data);
$code = $this->put(base64_encode($this->username));
if($code != 250 && $code != 334)
throw new CException($this->return);
$code = $this->put(base64_encode($this->password));
if($code != 250 && $code != 334)
throw new CException($this->return);
}
/**
* put a command to smtp server
*
* @param string $cmd
*
* @return integer status code
*/
protected function put($cmd)
{
fputs($this->_fp, $cmd . $this->crlf);
$this->return = '';
while($line = fgets($this->_fp, 128))
{
$this->return .= $line;
if(trim(substr($line, 3, 1)) == '')
break;
}
$this->code = substr($this->return, 0 , 3);
return $this->code;
}
}