PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)

smtp.php

  1   
  2class smtp 
  3
  4{ 
  5
  6/* Public Variables */ 
  7
  8var $smtp_port; 
  9
 10var $time_out; 
 11
 12var $host_name; 
 13
 14var $log_file; 
 15
 16var $relay_host; 
 17
 18var $debug; 
 19
 20var $auth; 
 21
 22var $user; 
 23
 24var $pass; 
 25
 26/* Private Variables */   
 27var $sock; 
 28
 29/* Constractor */ 
 30
 31function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass) 
 32
 33{ 
 34
 35$this->debug = FALSE; 
 36
 37$this->smtp_port = $smtp_port; 
 38
 39$this->relay_host = $relay_host; 
 40
 41$this->time_out = 30; //is used in fsockopen()   
 42# 
 43
 44$this->auth = $auth;//auth 
 45
 46$this->user = $user; 
 47
 48$this->pass = $pass; 
 49
 50# 
 51
 52$this->host_name = "localhost"; //is used in HELO command   
 53$this->log_file = ""; 
 54
 55$this->sock = FALSE; 
 56
 57} 
 58
 59/* Main Function */ 
 60
 61function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") 
 62
 63{ 
 64
 65$mail_from = $this->get_address($this->strip_comment($from)); 
 66
 67$body = ereg_replace("(^|(\r\n))(\\.)", "\1.\3", $body); 
 68
 69$header .= "MIME-Version:1.0\r\n"; 
 70
 71if($mailtype=="HTML"){ 
 72
 73$header .= "Content-Type:text/html\r\n"; 
 74
 75} 
 76
 77$header .= "To: ".$to."\r\n"; 
 78
 79if ($cc != "") { 
 80
 81$header .= "Cc: ".$cc."\r\n"; 
 82
 83} 
 84
 85$header .= "From: $from<".$from.">\r\n"; 
 86
 87$header .= "Subject: ".$subject."\r\n"; 
 88
 89$header .= $additional_headers; 
 90
 91$header .= "Date: ".date("r")."\r\n"; 
 92
 93$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n"; 
 94
 95list($msec, $sec) = explode(" ", microtime()); 
 96
 97$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n"; 
 98
 99$TO = explode(",", $this->strip_comment($to)); 
100
101if ($cc != "") { 
102
103$TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 
104
105} 
106
107if ($bcc != "") { 
108
109$TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 
110
111} 
112
113$sent = TRUE; 
114
115foreach ($TO as $rcpt_to) { 
116
117$rcpt_to = $this->get_address($rcpt_to); 
118
119if (!$this->smtp_sockopen($rcpt_to)) { 
120
121$this->log_write("Error: Cannot send email to ".$rcpt_to."\n"); 
122
123$sent = FALSE; 
124
125continue; 
126
127} 
128
129if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 
130
131$this->log_write("E-mail has been sent to <".$rcpt_to.">\n"); 
132
133} else { 
134
135$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n"); 
136
137$sent = FALSE; 
138
139} 
140
141fclose($this->sock); 
142
143$this->log_write("Disconnected from remote host\n"); 
144
145} 
146
147return $sent; 
148
149} 
150
151/* Private Functions */ 
152
153function smtp_send($helo, $from, $to, $header, $body = "") 
154
155{ 
156
157if (!$this->smtp_putcmd("HELO", $helo)) { 
158
159return $this->smtp_error("sending HELO command"); 
160
161} 
162
163#auth 
164
165if($this->auth){ 
166
167if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 
168
169return $this->smtp_error("sending HELO command"); 
170
171} 
172
173if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 
174
175return $this->smtp_error("sending HELO command"); 
176
177} 
178
179} 
180
181# 
182
183if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) { 
184
185return $this->smtp_error("sending MAIL FROM command"); 
186
187} 
188
189if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) { 
190
191return $this->smtp_error("sending RCPT TO command"); 
192
193} 
194
195if (!$this->smtp_putcmd("DATA")) { 
196
197return $this->smtp_error("sending DATA command"); 
198
199} 
200
201if (!$this->smtp_message($header, $body)) { 
202
203return $this->smtp_error("sending message"); 
204
205} 
206
207if (!$this->smtp_eom()) { 
208
209return $this->smtp_error("sending

<cr><lf>.<cr><lf> [EOM]");

}

if (!$this-&gt;smtp_putcmd("QUIT")) {

return $this-&gt;smtp_error("sending QUIT command");

}

return TRUE;

}

function smtp_sockopen($address)

{

if ($this-&gt;relay_host == "") {

return $this-&gt;smtp_sockopen_mx($address);

} else {

return $this-&gt;smtp_sockopen_relay();

}

}

function smtp_sockopen_relay()

{

$this-&gt;log_write("Trying to ".$this-&gt;relay_host.":".$this-&gt;smtp_port."\n");

$this-&gt;sock = @fsockopen($this-&gt;relay_host, $this-&gt;smtp_port, $errno, $errstr, $this-&gt;time_out);

if (!($this-&gt;sock &amp;&amp; $this-&gt;smtp_ok())) {

$this-&gt;log_write("Error: Cannot connenct to relay host ".$this-&gt;relay_host."\n");

$this-&gt;log_write("Error: ".$errstr." (".$errno.")\n");

return FALSE;

}

$this-&gt;log_write("Connected to relay host ".$this-&gt;relay_host."\n");

return TRUE;;

}

function smtp_sockopen_mx($address)

{

$domain = ereg_replace(" ^.+@([^@]+)$ ", "\1", $address);

if ( !@getmxrr($domain , $MXHOSTS)) {

$this-&gt;log_write("Error: Cannot resolve MX "".$domain.""\n");

return FALSE;

}

foreach ($MXHOSTS as $host) {

$this-&gt;log_write("Trying to ".$host.":".$this-&gt;smtp_port."\n");

$this-&gt;sock = @fsockopen($host, $this-&gt;smtp_port, $errno, $errstr, $this-&gt;time_out);

if (!($this-&gt;sock &amp;&amp; $this-&gt;smtp_ok())) {

$this-&gt;log_write("Warning: Cannot connect to mx host ".$host."\n");

$this-&gt;log_write("Error: ".$errstr." (".$errno.")\n");

continue;

}

$this-&gt;log_write("Connected to mx host ".$host."\n");

return TRUE;

}

$this-&gt;log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");

return FALSE;

}

function smtp_message($header, $body)

{

fputs($this-&gt;sock, $header."\r\n".$body);

$this-&gt;smtp_debug("&gt; ".str_replace("\r\n", "\n"."&gt; ", $header."\n&gt; ".$body."\n&gt; "));

return TRUE;

}

function smtp_eom()

{

fputs($this-&gt;sock, "\r\n.\r\n");

$this-&gt;smtp_debug(". [EOM]\n");

return $this-&gt;smtp_ok();

}

function smtp_ok()

{

$response = str_replace("\r\n", "", fgets($this-&gt;sock, 512));

$this-&gt;smtp_debug($response."\n");

if (!ereg("^[23]", $response)) {

fputs($this-&gt;sock, "QUIT\r\n");

fgets($this-&gt;sock, 512);

$this-&gt;log_write("Error: Remote host returned "".$response.""\n");

return FALSE;

}

return TRUE;

}

function smtp_putcmd($cmd, $arg = "")

{

if ($arg != "") {

if($cmd=="") $cmd = $arg;

else $cmd = $cmd." ".$arg;

}

fputs($this-&gt;sock, $cmd."\r\n");

$this-&gt;smtp_debug("&gt; ".$cmd."\n");

return $this-&gt;smtp_ok();

}

function smtp_error($string)

{

$this-&gt;log_write("Error: Error occurred while ".$string.".\n");

return FALSE;

}

function log_write($message)

{

$this-&gt;smtp_debug($message);

if ($this-&gt;log_file == "") {

return TRUE;

}

$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;

if ( !@file_exists($this-&gt;log_file ) || !($fp = @fopen($this-&gt;log_file, "a"))) {

$this-&gt;smtp_debug("Warning: Cannot open log file "".$this-&gt;log_file.""\n");

return FALSE;;

}

flock($fp, LOCK_EX);

fputs($fp, $message);

fclose($fp);

return TRUE;

}

function strip_comment($address)

{

$comment = "\([^()]*\)";

while (ereg($comment, $address)) {

$address = ereg_replace($comment, "", $address);

}

return $address;

}

function get_address($address)

{

$address = ereg_replace("([ \t\r\n])+", "", $address);

$address = ereg_replace("^.&lt;(.+)&gt;.$", "\1", $address);

return $address;

}

function smtp_debug($message)

{

if ($this-&gt;debug) {

echo $message;

}

}

}

1
2test.php 

/*

这是一个测试程序!!!

请按照说明设置好以下的参数,以下是以tom.com的用户为例设置好的.

*/

require("sm.php");

##########################################

$smtpserver = "smtp.tom.com";//SMTP服务器

$smtpserverport =25;//SMTP服务器端口

$smtpusermail = " [email protected]";//SMTP 服务器的用户邮箱

$smtpemailto = " [email protected]";// 发送给谁

$smtpuser = "someone";//SMTP服务器的用户帐号

$smtppass = "someonepass";//SMTP服务器的用户密码

$mailsubject = "Test Subject";//邮件主题

$mailbody = "<h1>This is a test mail</h1>";//邮件内容

$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件

##########################################

$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.

$smtp-&gt;debug = TRUE;//是否显示发送的调试信息

$smtp-&gt;sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

Published At
Categories with Web编程
Tagged with
comments powered by Disqus