php使用smtp方式发邮件

PHP代码
  1. <?php  
  2. class smtp  
  3. {  
  4. var $smtp_port;  
  5. var $time_out;  
  6. var $host_name;  
  7. var $log_file;  
  8. var $relay_host;  
  9. var $debug;  
  10. var $auth;  
  11. var $user;  
  12. var $pass;  
  13. var $sock;  
  14. function smtp($relay_host = ""$smtp_port = 25,$auth = false,$user,$pass)  
  15. {  
  16. $this->debug = true;  
  17. $this->smtp_port = $smtp_port;  
  18. $this->relay_host = $relay_host;  
  19. $this->time_out = 30;  
  20. $this->auth = $auth;  
  21. $this->user = $user;  
  22. $this->pass = $pass;  
  23. $this->host_name = "localhost";  
  24. $this->log_file ="";  
  25. $this->sock = FALSE;  
  26. }  
  27. function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = "")  
  28. {  
  29. $mail_from = $this->get_address($this->strip_comment($from));  
  30. $body = ereg_replace("(^|(\r\n))(\\.)""\\1.\\3"$body);  
  31. $header .= "MIME-Version:1.0\r\n";  
  32. if($mailtype=="HTML")  
  33. {  
  34. $header .= "Content-Type:text/html\r\n";  
  35. }  
  36.  $header .= "To: ".$to."\r\n";  
  37. if ($cc != "")  
  38. {  
  39. $header .= "Cc: ".$cc."\r\n";  
  40. }  
  41. $header .= "From: $from<".$from.">\r\n";  
  42. $header .= "Subject: ".$subject."\r\n";  
  43. $header .= $additional_headers;  
  44. $header .= "Date: ".date("r")."\r\n";  
  45. $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";  
  46. list($msec$sec) = explode(" ", microtime());  
  47. $header .= "Message-ID: <".date("YmdHis"$sec).".".($msec*1000000).".".$mail_from.">\r\n";  
  48. $TO = explode(","$this->strip_comment($to));  
  49.    
  50. if ($cc != "") {  
  51. $TO = array_merge($TOexplode(","$this->strip_comment($cc)));  
  52. }  
  53. if ($bcc != "") {  
  54. $TO = array_merge($TOexplode(","$this->strip_comment($bcc)));  
  55. }  
  56.   
  57.    
  58. $sent = TRUE;  
  59. foreach ($TO as $rcpt_to) {  
  60. $rcpt_to = $this->get_address($rcpt_to);  
  61. if (!$this->smtp_sockopen($rcpt_to)) {  
  62. $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");  
  63. $sent = FALSE;  
  64. continue;  
  65. }  
  66. if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) {  
  67. $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");  
  68. else {  
  69. $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");  
  70. $sent = FALSE;  
  71. }  
  72. fclose($this->sock);  
  73. $this->log_write("Disconnected from remote host\n");  
  74. }  
  75. //echo "<br>";  
  76. //echo $header;  
  77. return $sent;  
  78. }  
  79.    
  80. /* Private Functions */  
  81.    
  82. function smtp_send($helo$from$to$header$body = "")  
  83. {  
  84. if (!$this->smtp_putcmd("HELO"$helo)) {  
  85. return $this->smtp_error("sending HELO command");  
  86. }  
  87. #auth  
  88. if($this->auth){  
  89. if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) {  
  90. return $this->smtp_error("sending HELO command");  
  91. }  
  92.    
  93. if (!$this->smtp_putcmd(""base64_encode($this->pass))) {  
  94. return $this->smtp_error("sending HELO command");  
  95. }  
  96. }  
  97. if (!$this->smtp_putcmd("MAIL""FROM:<".$from.">")) {  
  98. return $this->smtp_error("sending MAIL FROM command");  
  99. }  
  100.    
  101. if (!$this->smtp_putcmd("RCPT""TO:<".$to.">")) {  
  102. return $this->smtp_error("sending RCPT TO command");  
  103. }  
  104.    
  105. if (!$this->smtp_putcmd("DATA")) {  
  106. return $this->smtp_error("sending DATA command");  
  107. }  
  108.    
  109. if (!$this->smtp_message($header$body)) {  
  110. return $this->smtp_error("sending message");  
  111. }  
  112.    
  113. if (!$this->smtp_eom()) {  
  114. return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");  
  115. }  
  116.    
  117. if (!$this->smtp_putcmd("QUIT")) {  
  118. return $this->smtp_error("sending QUIT command");  
  119. }  
  120.    
  121. return TRUE;  
  122. }  
  123.    
  124. function smtp_sockopen($address)  
  125. {  
  126. if ($this->relay_host == "") {  
  127. return $this->smtp_sockopen_mx($address);  
  128. else {  
  129. return $this->smtp_sockopen_relay();  
  130. }  
  131. }  
  132.   
  133.    
  134. function smtp_sockopen_relay()  
  135. {  
  136. $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");  
  137. $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out);  
  138. if (!($this->sock && $this->smtp_ok())) {  
  139. $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");  
  140. $this->log_write("Error: ".$errstr." (".$errno.")\n");  
  141. return FALSE;  
  142. }  
  143. $this->log_write("Connected to relay host ".$this->relay_host."\n");  
  144. return TRUE;;  
  145. }  
  146.   
  147.    
  148. function smtp_sockopen_mx($address)  
  149. {  
  150. $domain = ereg_replace("^.+@([^@]+)$""\\1"$address);  
  151. if (!@getmxrr($domain$MXHOSTS)) {  
  152. $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");  
  153. return FALSE;  
  154. }  
  155. foreach ($MXHOSTS as $host) {  
  156. $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");  
  157. $this->sock = @fsockopen($host$this->smtp_port, $errno$errstr$this->time_out);  
  158. if (!($this->sock && $this->smtp_ok())) {  
  159. $this->log_write("Warning: Cannot connect to mx host ".$host."\n");  
  160. $this->log_write("Error: ".$errstr." (".$errno.")\n");  
  161. continue;  
  162. }  
  163. $this->log_write("Connected to mx host ".$host."\n");  
  164. return TRUE;  
  165. }  
  166. $this->log_write("Error: Cannot connect to any mx hosts (".implode(", "$MXHOSTS).")\n");  
  167. return FALSE;  
  168. }  
  169.    
  170. function smtp_message($header$body)  
  171. {  
  172. fputs($this->sock, $header."\r\n".$body);  
  173. $this->smtp_debug("> ".str_replace("\r\n""\n"."> "$header."\n> ".$body."\n> "));  
  174.    
  175. return TRUE;  
  176. }  
  177.    
  178. function smtp_eom()  
  179. {  
  180. fputs($this->sock, "\r\n.\r\n");  
  181. $this->smtp_debug(". [EOM]\n");  
  182.    
  183. return $this->smtp_ok();  
  184. }  
  185.    
  186. function smtp_ok()  
  187. {  
  188. $response = str_replace("\r\n"""fgets($this->sock, 512));  
  189. $this->smtp_debug($response."\n");  
  190.    
  191. if (!ereg("^[23]"$response)) {  
  192. fputs($this->sock, "QUIT\r\n");  
  193. fgets($this->sock, 512);  
  194. $this->log_write("Error: Remote host returned \"".$response."\"\n");  
  195. return FALSE;  
  196. }  
  197. return TRUE;  
  198. }  
  199.    
  200. function smtp_putcmd($cmd$arg = "")  
  201. {  
  202. if ($arg != "") {  
  203. if($cmd==""$cmd = $arg;  
  204. else $cmd = $cmd." ".$arg;  
  205. }  
  206.   
  207.    
  208. fputs($this->sock, $cmd."\r\n");  
  209. $this->smtp_debug("> ".$cmd."\n");  
  210.    
  211. return $this->smtp_ok();  
  212. }  
  213.    
  214. function smtp_error($string)  
  215. {  
  216. $this->log_write("Error: Error occurred while ".$string.".\n");  
  217. return FALSE;  
  218. }  
  219.    
  220. function log_write($message)  
  221. {  
  222. $this->smtp_debug($message);  
  223.    
  224. if ($this->log_file == "") {  
  225. return TRUE;  
  226. }  
  227.    
  228. $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;  
  229. if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {  
  230. $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");  
  231. return FALSE;  
  232. }  
  233. flock($fp, LOCK_EX);  
  234. fputs($fp$message);  
  235. fclose($fp);  
  236.    
  237. return TRUE;  
  238. }  
  239.    
  240. function strip_comment($address)  
  241. {  
  242. $comment = "\\([^()]*\\)";  
  243. while (ereg($comment$address)) {  
  244. $address = ereg_replace($comment""$address);  
  245. }  
  246.    
  247. return $address;  
  248. }  
  249.    
  250. function get_address($address)  
  251. {  
  252. $address = ereg_replace("([ \t\r\n])+"""$address);  
  253. $address = ereg_replace("^.*<(.+)>.*$""\\1"$address);  
  254.    
  255. return $address;  
  256. }  
  257.    
  258. function smtp_debug($message)  
  259. {  
  260. if ($this->debug) {  
  261. echo $message."<br>";  
  262. }  
  263. }  
  264.    
  265. function get_attach_type($image_tag) { //  
  266.    
  267. $filedata = array();  
  268.    
  269. $img_file_con=fopen($image_tag,"r");  
  270. unset($image_data);  
  271. while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))  
  272. $image_data.=$tem_buffer;  
  273. fclose($img_file_con);  
  274.    
  275. $filedata['context'] = $image_data;  
  276. $filedata['filename']= basename($image_tag);  
  277. $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));  
  278. switch($extension){  
  279. case ".gif":  
  280. $filedata['type'] = "image/gif";  
  281. break;  
  282. case ".gz":  
  283. $filedata['type'] = "application/x-gzip";  
  284. break;  
  285. case ".htm":  
  286. $filedata['type'] = "text/html";  
  287. break;  
  288. case ".html":  
  289. $filedata['type'] = "text/html";  
  290. break;  
  291. case ".jpg":  
  292. $filedata['type'] = "image/jpeg";  
  293. break;  
  294. case ".tar":  
  295. $filedata['type'] = "application/x-tar";  
  296. break;  
  297. case ".txt":  
  298. $filedata['type'] = "text/plain";  
  299. break;  
  300. case ".zip":  
  301. $filedata['type'] = "application/zip";  
  302. break;  
  303. default:  
  304. $filedata['type'] = "application/octet-stream";  
  305. break;  
  306. }  
  307.    
  308.    
  309. return $filedata;  
  310. }  
  311.  }  
  312. $smtpserver = "smtp.qq.com";//SMTP服务器  
  313. $smtpserverport =25;//SMTP服务器端口  
  314. $smtpusermail = "";//SMTP服务器的用户邮箱  
  315. $smtpemailto = "";//发送给谁  
  316. $smtpuser = "";//SMTP服务器的用户帐号  
  317. $smtppass = "";//SMTP服务器的用户密码  
  318. $mailsubject = "";//邮件主题  
  319. $mailbody = '';//邮件内容  
  320. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件  
  321. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);  
  322. $smtp->debug = false;//是否显示发送的调试信息  
  323. $smtp->sendmail($smtpemailto$smtpusermail$mailsubject$mailbody$mailtype);  
  324. if($smtp==true){  
  325. echo '发送成功!';  
  326. }  
  327. else{  
  328. echo '发送失败!';  
  329. }  
  330. ?>  


[本日志由 admin 于 2012-05-25 10:14 AM 更新]
上一篇: PHP正则验证表单类
下一篇: php获得网站根目录
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2809
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑