当前位置:Gxlcms > PHP教程 > PHP邮件操作类

PHP邮件操作类

时间:2021-07-01 10:21:17 帮助过:32人阅读

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

    你的用户名是张三,密码是11111

    ";//邮件内容
  278. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  279. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
  280. $smtp->debug = true;//是否显示发送的调试信息
  281. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  282. ?>

PHP

人气教程排行