Home Backend Development PHP Tutorial PHP mail operation class

PHP mail operation class

Jul 25, 2016 am 08:42 AM

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

PHP


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles