1
<?php2
class smtp 3
{ 4
/* Public Variables */ 5
var $smtp_port; 6
var $time_out; 7
var $host_name; 8
var $log_file; 9
var $relay_host; 10
var $debug; 11
var $auth; 12
var $user; 13
var $pass; 14
/* Private Variables */ 15
var $sock; 16
/* Constractor */ 17
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 18
{ 19
$this->debug = FALSE; 20
$this->smtp_port = $smtp_port; 21
$this->relay_host = $relay_host; 22
$this->time_out = 30; //is used in fsockopen() 23
# 24
$this->auth = $auth;//auth 25
$this->user = $user; 26
$this->pass = $pass; 27
# 28
$this->host_name = "localhost"; //is used in HELO command 29
$this->log_file = ""; 30
$this->sock = FALSE; 31
} 32
/* Main Function */ 33
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 34
{ 35
$mail_from = $this->get_address($this->strip_comment($from)); 36
$body = ereg_replace("(^(\r\n))(\.)", "\1.\3", $body); 37
$header = "MIME-Version:1.0\r\n"; 38
if($mailtype=="HTML"){ 39
$header .= "Content-Type:text/html\r\n"; 40
} 41
$header .= "To: ".$to."\r\n"; 42
if ($cc != "") { 43
$header .= "Cc: ".$cc."\r\n"; 44
} 45
$header .= "From: $from<".$from.">\r\n"; 46
$header .= "Subject: ".$subject."\r\n"; 47
$header .= $additional_headers; 48
$header .= "Date: ".date("r")."\r\n"; 49
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 50
list($msec, $sec) = explode(" ", microtime()); 51
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 52
$TO = explode(",", $this->strip_comment($to)); 53
if ($cc != "") { 54
$TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 55
} 56
if ($bcc != "") { 57
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 58
} 59
$sent = TRUE; 60
foreach ($TO as $rcpt_to) { 61
$rcpt_to = $this->get_address($rcpt_to); 62
if (!$this->smtp_sockopen($rcpt_to)) { 63
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 64
$sent = FALSE; 65
continue; 66
} 67
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 68
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 69
} else { 70
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 71
$sent = FALSE; 72
} 73
fclose($this->sock); 74
$this->log_write("Disconnected from remote host\n"); 75
} 76
return $sent; 77
} 78
/* Private Functions */ 79
function smtp_send($helo, $from, $to, $header, $body = "") 80
{ 81
if (!$this->smtp_putcmd("HELO", $helo)) { 82
return $this->smtp_error("sending HELO command"); 83
} 84
#auth 85
if($this->auth){ 86
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 87
return $this->smtp_error("sending HELO command"); 88
} 89
if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 90
return $this->smtp_error("sending HELO command"); 91
} 92
} 93
# 94
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 95
return $this->smtp_error("sending MAIL FROM command"); 96
} 97
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 98
return $this->smtp_error("sending RCPT TO command"); 99
} 100
if (!$this->smtp_putcmd("DATA")) { 101
return $this->smtp_error("sending DATA command"); 102
} 103
if (!$this->smtp_message($header, $body)) { 104
return $this->smtp_error("sending message"); 105
} 106
if (!$this->smtp_eom()) { 107
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 108
} 109
if (!$this->smtp_putcmd("QUIT")) { 110
return $this->smtp_error("sending QUIT command"); 111
} 112
return TRUE; 113
} 114
function smtp_sockopen($address) 115
{ 116
if ($this->relay_host == "") { 117
return $this->smtp_sockopen_mx($address); 118
} else { 119
return $this->smtp_sockopen_relay(); 120
} 121
} 122
function smtp_sockopen_relay() 123
{ 124
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n"); 125
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 126
if (!($this->sock && $this->smtp_ok())) { 127
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n"); 128
$this->log_write("Error: ".$errstr." (".$errno.")\n"); 129
return FALSE; 130
} 131
$this->log_write("Connected to relay host ".$this->relay_host."\n"); 132
return TRUE; 133
} 134

135
function smtp_sockopen_mx($address) 136
{ 137
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address); 138
if (!@getmxrr($domain, $MXHOSTS)) { 139
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n"); 140
return FALSE; 141
} 142
foreach ($MXHOSTS as $host) { 143
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n"); 144
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 145
if (!($this->sock && $this->smtp_ok())) { 146
$this->log_write("Warning: Cannot connect to mx host ".$host."\n"); 147
$this->log_write("Error: ".$errstr." (".$errno.")\n"); 148
continue; 149
} 150
$this->log_write("Connected to mx host ".$host."\n"); 151
return TRUE; 152
} 153
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n"); 154
return FALSE; 155
} 156

157
function smtp_message($header, $body) 158
{ 159
fputs($this->sock, $header."\r\n".$body); 160
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> ")); 161
return TRUE; 162
} 163

164
function smtp_eom() 165
{ 166
fputs($this->sock, "\r\n.\r\n"); 167
$this->smtp_debug(". [EOM]\n"); 168
return $this->smtp_ok(); 169
} 170

171
function smtp_ok() 172
{ 173
$response = str_replace("\r\n", "", fgets($this->sock, 512)); 174
$this->smtp_debug($response."\n"); 175
if (!ereg("^[23]", $response)) { 176
fputs($this->sock, "QUIT\r\n"); 177
fgets($this->sock, 512); 178
$this->log_write("Error: Remote host returned \"".$response."\"\n"); 179
return FALSE; 180
} 181
return TRUE; 182
} 183

184
function smtp_putcmd($cmd, $arg = "") 185
{ 186
if ($arg != "") { 187
if($cmd=="") $cmd = $arg; 188
else $cmd = $cmd." ".$arg; 189
} 190
fputs($this->sock, $cmd."\r\n"); 191
$this->smtp_debug("> ".$cmd."\n"); 192
return $this->smtp_ok(); 193
} 194

195
function smtp_error($string) 196
{ 197
$this->log_write("Error: Error occurred while ".$string.".\n"); 198
return FALSE; 199
} 200

201
function log_write($message) 202
{ 203
$this->smtp_debug($message); 204
if ($this->log_file == "") { 205
return TRUE; 206
} 207
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message; 208
if (!@file_exists($this->log_file) ||($fp = @fopen($this->log_file, "a"))) { 209
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n"); 210
return FALSE;; 211
} 212
flock($fp, LOCK_EX); 213
fputs($fp, $message); 214
fclose($fp); 215
return TRUE; 216
} 217

218
function strip_comment($address) 219
{ 220
$comment = "\([^()]*\)"; 221
while (ereg($comment, $address)) { 222
$address = ereg_replace($comment, "", $address); 223
} 224
return $address; 225
} 226

227
function get_address($address) 228
{ 229
$address = ereg_replace("([ \t\r\n])+", "", $address); 230
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address); 231
return $address; 232
} 233

234
function smtp_debug($message) 235
{ 236
if ($this->debug) { 237
echo $message; 238
} 239
} 240
} 241
?>1
<? 2
require (FILE_DIR."smtp.php"); 3
$smtpserver = "smtp.xxx.com";//SMTP服务器 4
$smtpserverport =25;//SMTP服务器端口 5
$smtpusermail = "xxxxx@xxx.com";//SMTP服务器的用户邮箱 6
$smtpemailto = "aaaaa@xxx.com";//发送给谁 7
$smtpuser = "xxxxx";//SMTP服务器的用户帐号 8
$smtppass = "xxxxx";//SMTP服务器的用户密码 9
$mailsubject = "Test Subject";//邮件主题 10
$mailbody = "<h1>This is a test mail</h1>";//邮件内容 11
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 12
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. 13
$smtp->debug = TRUE;//是否显示发送的调试信息 14
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 15
?> 来源:http://blog.163.com/gu__zeng/blog/static/137631252008850025353/