设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 107|回复: 0

A simple php smtp class

[复制链接]
发表于 2014-1-3 15:18:23 | 显示全部楼层 |阅读模式
smtp.class.php
  1. <?php

  2. define('SMTP_STATUS_NOT_CONNECTED', 1, TRUE);
  3. define('SMTP_STATUS_CONNECTED', 2, TRUE);

  4. class smtp
  5. {

  6. var $connection;
  7. var $recipients;
  8. var $headers;
  9. var $timeout;
  10. var $errors;
  11. var $status;
  12. var $body;
  13. var $from;
  14. var $host;
  15. var $port;
  16. var $helo;
  17. var $auth;
  18. var $user;
  19. var $pass;
  20. var $debug;

  21. /**
  22. * 参数为一个数组
  23. * host SMTP 服务器的主机 默认:localhost
  24. * port SMTP 服务器的端口 默认:25
  25. * helo 发送HELO命令的名称 默认:localhost
  26. * user SMTP 服务器的用户名 默认:空值
  27. * pass SMTP 服务器的登陆密码 默认:空值
  28. * timeout 连接超时的时间 默认:5
  29. * @return bool
  30. */

  31. function smtp($params = array())
  32. {

  33. if(!defined('CRLF')) define('CRLF', “\r\n”, TRUE);

  34. $this->timeout = 5;
  35. $this->status = SMTP_STATUS_NOT_CONNECTED;
  36. $this->host = ‘localhost';
  37. $this->port = 25;
  38. $this->auth = FALSE;
  39. $this->user = ”;
  40. $this->pass = ”;
  41. $this->errors = array();
  42. $this->debug = false;
  43. foreach($params as $key => $value)
  44. {
  45. $this->$key = $value;
  46. }

  47. $this->helo = $this->host;

  48. // 如果没有设置用户名则不验证
  49. $this->auth = (” == $this->user) ? FALSE : TRUE;
  50. }

  51. function connect($params = array())
  52. {

  53. if(!isset($this->status))
  54. {
  55. $obj = new smtp($params);

  56. if($obj->connect())
  57. {
  58. $obj->status = SMTP_STATUS_CONNECTED;
  59. }

  60. return $obj;

  61. }
  62. else
  63. {

  64. $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  65. socket_set_timeout($this->connection, 0, 250000);

  66. $greeting = $this->get_data();

  67. if(is_resource($this->connection))
  68. {
  69. $this->status = 2;
  70. return $this->auth ? $this->ehlo() : $this->helo();
  71. }
  72. else
  73. {
  74. $this->errors[] = ‘Failed to connect to server: ‘.$errstr;
  75. return FALSE;
  76. }
  77. }
  78. }

  79. /**
  80. * 参数为数组
  81. * recipients 接收人的数组
  82. * from 发件人的地址,也将作为回复地址
  83. * headers 头部信息的数组
  84. * body 邮件的主体
  85. */

  86. function send($params = array())
  87. {

  88. foreach($params as $key => $value)
  89. {
  90. $this->set($key, $value);
  91. }

  92. if($this->is_connected())
  93. {
  94. // 服务器是否需要验证
  95. if($this->auth)
  96. {
  97. if(!$this->auth()) return FALSE;
  98. }

  99. $this->mail($this->from);

  100. if(is_array($this->recipients))
  101. {
  102. foreach($this->recipients as $value)
  103. {
  104. $this->rcpt($value);
  105. }
  106. }
  107. else
  108. {
  109. $this->rcpt($this->recipients);
  110. }

  111. if(!$this->data()) return FALSE;

  112. $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers)));
  113. $body = str_replace(CRLF.'.', CRLF.'..', $this->body);
  114. $body = $body[0] == ‘.' ? ‘.'.$body : $body;

  115. $this->send_data($headers);
  116. $this->send_data(”);
  117. $this->send_data($body);
  118. $this->send_data('.');

  119. return (substr(trim($this->get_data()), 0, 3) === ‘250′);
  120. }
  121. else
  122. {
  123. $this->errors[] = ‘Not connected!';
  124. return FALSE;
  125. }
  126. }

  127. function helo()
  128. {
  129. if(is_resource($this->connection)
  130. AND $this->send_data('HELO ‘.$this->helo)
  131. AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
  132. {
  133. return TRUE;

  134. }
  135. else
  136. {
  137. $this->errors[] = ‘HELO command failed, output: ‘ . trim(substr(trim($error),3));
  138. return FALSE;
  139. }
  140. }

  141. function ehlo()
  142. {
  143. if(is_resource($this->connection)
  144. AND $this->send_data('EHLO ‘.$this->helo)
  145. AND substr(trim($error = $this->get_data()), 0, 3) === ‘250′ )
  146. {
  147. return TRUE;
  148. }
  149. else
  150. {
  151. $this->errors[] = ‘EHLO command failed, output: ‘ . trim(substr(trim($error),3));
  152. return FALSE;
  153. }
  154. }

  155. function auth()
  156. {
  157. if(is_resource($this->connection)
  158. AND $this->send_data('AUTH LOGIN')
  159. AND substr(trim($error = $this->get_data()), 0, 3) === ‘334′
  160. AND $this->send_data(base64_encode($this->user)) // Send username
  161. AND substr(trim($error = $this->get_data()),0,3) === ‘334′
  162. AND $this->send_data(base64_encode($this->pass)) // Send password
  163. AND substr(trim($error = $this->get_data()),0,3) === ‘235′ )
  164. {
  165. return TRUE;
  166. }
  167. else
  168. {
  169. $this->errors[] = ‘AUTH command failed: ‘ . trim(substr(trim($error),3));
  170. return FALSE;
  171. }
  172. }

  173. function mail($from)
  174. {

  175. if($this->is_connected()
  176. AND $this->send_data('MAIL FROM:<'.$from.'>')
  177. AND substr(trim($this->get_data()), 0, 2) === ‘250′ )
  178. {
  179. return TRUE;
  180. }
  181. else
  182. {
  183. return FALSE;
  184. }
  185. }

  186. function rcpt($to)
  187. {
  188. if($this->is_connected()
  189. AND $this->send_data('RCPT TO:<'.$to.'>')
  190. AND substr(trim($error = $this->get_data()), 0, 2) === ‘25′ )
  191. {
  192. return TRUE;
  193. }
  194. else
  195. {
  196. $this->errors[] = trim(substr(trim($error), 3));
  197. return FALSE;
  198. }
  199. }

  200. function data()
  201. {

  202. if($this->is_connected()
  203. AND $this->send_data('DATA')
  204. AND substr(trim($error = $this->get_data()), 0, 3) === ‘354′ )
  205. {
  206. return TRUE;
  207. }
  208. else
  209. {
  210. $this->errors[] = trim(substr(trim($error), 3));
  211. return FALSE;
  212. }
  213. }

  214. function is_connected()
  215. {
  216. return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED));
  217. }

  218. function send_data($data)
  219. {
  220. if(is_resource($this->connection))
  221. {
  222. if($this->debug)
  223. echo nl2br($data.CRLF);
  224. return fwrite($this->connection, $data.CRLF, strlen($data)+2);
  225. }
  226. else
  227. {
  228. return FALSE;
  229. }
  230. }

  231. function &get_data()
  232. {

  233. $return = ”;
  234. $line = ”;

  235. if(is_resource($this->connection))
  236. {
  237. while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ‘ ‘)
  238. {
  239. $line = fgets($this->connection, 512);
  240. $return .= $line;
  241. }
  242. if($this->debug===true)
  243. echo nl2br($return.CRLF);
  244. return $return;

  245. }
  246. else
  247. {
  248. return FALSE;
  249. }
  250. }

  251. function set($var, $value)
  252. {
  253. $this->$var = $value;
  254. return TRUE;
  255. }
  256. } // End of class
  257. ?>
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 22:30 , Processed in 0.013845 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表