Phpsmtp sends an email

Source: Internet
Author: User
Tags ereg
Phpsmtp sends an email

Php needs to send mail via smtp through a class smtp

The code for sending an email is as follows:

  1. Require_once 'smtp. php ';
  2. ######################################## ##
  3. $ Smtpserver = "smtp.sina.com"; // SMTP server
  4. $ Smtpserverport = 25; // SMTP server port
  5. $ Smtpusermail = "god_chen@sina.com"; // user email address of the SMTP server
  6. $ Smtpemailto = "45323333@qq.com"; // to whom to send
  7. $ Smtpuser = "name"; // User account of the SMTP server
  8. $ Smtppass = "123456"; // SMTP server user password
  9. $ Mailsubject = "Chinese"; // email subject
  10. $ Mailbody = "New Year's sensation in Chinese test"; // email content
  11. $ Mailtype = "HTML"; // The email format (HTML/TXT). TXT is a text email.
  12. ######################################## ##
  13. $ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // Here, true indicates that authentication is used; otherwise, authentication is not used.
  14. $ Smtp-> debug = FALSE; // whether to display the sent debugging information
  15. $ Smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mailtype)




The smtp class source code is as follows:

  1. Class smtp
  2. {
  3. /* Public Variables */
  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. /* Private Variables */
  14. Var $ sock;
  15. /* Constractor */
  16. Function smtp ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass)
  17. {
  18. $ This-> debug = FALSE;
  19. $ This-> smtp_port = $ smtp_port;
  20. $ This-> relay_host = $ relay_host;
  21. $ This-> time_out = 30; // is used in fsockopen ()
  22. $ This-> auth = $ auth; // auth
  23. $ This-> user = $ user;
  24. $ This-> pass = $ pass;
  25. $ This-> host_name = "localhost"; // is used in HELO command
  26. $ This-> log_file = "";
  27. $ This-> sock = FALSE;
  28. }
  29. /* Main Function */
  30. Function sendmail ($ to, $ from, $ subject = "", $ body = "", $ mailtype, $ cc = "", $ bcc = "", $ additional_headers = "")
  31. {
  32. $ Mail_from = $ this-> get_address ($ this-> strip_comment ($ from ));
  33. $ Body = ereg_replace ("(^ | (\ r \ n) (\.)", "\ 1. \ 3", $ body );
  34. $ Header = "MIME-Version: 1.0 \ r \ n ";
  35. If ($ mailtype = "HTML ")
  36. {
  37. $ Header. = "Content-Type: text/html \ r \ n ";
  38. }
  39. $ Header. = "To:". $ to. "\ r \ n ";
  40. If ($ cc! = "")
  41. {
  42. $ Header. = "Cc:". $ cc. "\ r \ n ";
  43. }
  44. $ Header. = "From: $ from <". $ from. "> \ r \ n ";
  45. $ Header. = "Subject:". $ subject. "\ r \ n ";
  46. $ Header. = $ additional_headers;
  47. $ Header. = "Date:". date ("r"). "\ r \ n ";
  48. $ Header. = "X-Mailer: By Redhat (PHP/". phpversion (). ") \ r \ n ";
  49. List ($ msec, $ sec) = explode ("", microtime ());
  50. $ Header. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> \ r \ n ";
  51. $ TO = explode (",", $ this-> strip_comment ($ ));
  52. If ($ cc! = "")
  53. {
  54. $ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ cc )));
  55. }
  56. If ($ bcc! = "")
  57. {
  58. $ TO = array_merge ($ TO, explode (",", $ this-> strip_comment ($ bcc )));
  59. }
  60. $ Sent = TRUE;
  61. Foreach ($ TO as $ rcpt_to)
  62. {
  63. $ Rcpt_to = $ this-> get_address ($ rcpt_to );
  64. If (! $ This-> smtp_sockopen ($ rcpt_to ))
  65. {
  66. $ This-> log_write ("Error: Cannot send email to". $ rcpt_to. "\ n ");
  67. $ Sent = FALSE;
  68. Continue;
  69. }
  70. If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ header, $ body ))
  71. {
  72. $ This-> log_write ("E-mail has been sent to <". $ rcpt_to. "> \ n ");
  73. }
  74. Else
  75. {
  76. $ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> \ n ");
  77. $ Sent = FALSE;
  78. }
  79. Fclose ($ this-> sock );
  80. $ This-> log_write ("Disconnected from remote host \ n ");
  81. }
  82. Return $ sent;
  83. }
  84. /* Private Functions */
  85. Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
  86. {
  87. If (! $ This-> smtp_putcmd ("HELO", $ helo ))
  88. {
  89. Return $ this-> smtp_error ("sending HELO command ");
  90. }
  91. # Auth
  92. If ($ this-> auth)
  93. {
  94. If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user )))
  95. {
  96. Return $ this-> smtp_error ("sending HELO command ");
  97. }
  98. If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass )))
  99. {
  100. Return $ this-> smtp_error ("sending HELO command ");
  101. }
  102. }
  103. If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> "))
  104. {
  105. Return $ this-> smtp_error ("sending mail from command ");
  106. }
  107. If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> "))
  108. {
  109. Return $ this-> smtp_error ("sending rcpt to command ");
  110. }
  111. If (! $ This-> smtp_putcmd ("DATA "))
  112. {
  113. Return $ this-> smtp_error ("sending DATA command ");
  114. }
  115. If (! $ This-> smtp_message ($ header, $ body ))
  116. {
  117. Return $ this-> smtp_error ("sending message ");
  118. }
  119. If (! $ This-> smtp_eom ())
  120. {
  121. Return $ this-> smtp_error ("sending . [EOM] ");
  122. }
  123. If (! $ This-> smtp_putcmd ("QUIT "))
  124. {
  125. Return $ this-> smtp_error ("sending QUIT command ");
  126. }
  127. Return TRUE;
  128. }
  129. Function smtp_sockopen ($ address)
  130. {
  131. If ($ this-> relay_host = "")
  132. {
  133. Return $ this-> smtp_sockopen_mx ($ address );
  134. }
  135. Else
  136. {
  137. Return $ this-> smtp_sockopen_relay ();
  138. }
  139. }
  140. Function smtp_sockopen_relay ()
  141. {
  142. $ This-> log_write ("Trying to". $ this-> relay_host. ":". $ this-> smtp_port. "\ n ");
  143. $ This-> sock = @ fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  144. If (! ($ This-> sock & $ this-> smtp_ OK ()))
  145. {
  146. $ This-> log_write ("Error: Cannot connenct to relay host". $ this-> relay_host. "\ n ");
  147. $ This-> log_write ("Error:". $ errstr. "(". $ errno. ") \ n ");
  148. Return FALSE;
  149. }
  150. $ This-> log_write ("Connected to relay host". $ this-> relay_host. "\ n ");
  151. Return TRUE ;;
  152. }
  153. Function smtp_sockopen_mx ($ address)
  154. {
  155. $ Domain = ereg_replace ("^. + @ ([^ @] +) $", "\ 1", $ address );
  156. If (! @ Getmxrr ($ domain, $ MXHOSTS ))
  157. {
  158. $ This-> log_write ("Error: Cannot resolve MX \" ". $ domain." \ "\ n ");
  159. Return FALSE;
  160. }
  161. Foreach ($ MXHOSTS as $ host)
  162. {
  163. $ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port. "\ n ");
  164. $ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  165. If (! ($ This-> sock & $ this-> smtp_ OK ()))
  166. {
  167. $ This-> log_write ("Warning: Cannot connect to mx host". $ host. "\ n ");
  168. $ This-> log_write ("Error:". $ errstr. "(". $ errno. ") \ n ");
  169. Continue;
  170. }
  171. $ This-> log_write ("Connected to mx host". $ host. "\ n ");
  172. Return TRUE;
  173. }
  174. $ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS). ") \ n ");
  175. Return FALSE;
  176. }
  177. Function smtp_message ($ header, $ body)
  178. {
  179. Fputs ($ this-> sock, $ header. "\ r \ n". $ body );
  180. $ This-> smtp_debug ("> ". str_replace ("\ r \ n", "\ n ". ">", $ header. "\ n> ". $ body. "\ n> "));
  181. Return TRUE;
  182. }
  183. Function smtp_eom ()
  184. {
  185. Fputs ($ this-> sock, "\ r \ n. \ r \ n ");
  186. $ This-> smtp_debug (". [EOM] \ n ");
  187. Return $ this-> smtp_ OK ();
  188. }
  189. Function smtp_ OK ()
  190. {
  191. $ Response = str_replace ("\ r \ n", "", fgets ($ this-> sock, 512 ));
  192. $ This-> smtp_debug ($ response. "\ n ");
  193. If (! Ereg ("^ [23]", $ response ))
  194. {
  195. Fputs ($ this-> sock, "QUIT \ r \ n ");
  196. Fgets ($ this-> sock, 512 );
  197. $ This-> log_write ("Error: Remote host returned \" ". $ response." \ "\ n ");
  198. Return FALSE;
  199. }
  200. Return TRUE;
  201. }
  202. Function smtp_putcmd ($ cmd, $ arg = "")
  203. {
  204. If ($ arg! = "")
  205. {
  206. If ($ cmd = "")
  207. {
  208. $ Cmd = $ arg;
  209. }
  210. Else
  211. {
  212. $ Cmd = $ cmd. "". $ arg;
  213. }
  214. }
  215. Fputs ($ this-> sock, $ cmd. "\ r \ n ");
  216. $ This-> smtp_debug (">". $ cmd. "\ n ");
  217. Return $ this-> smtp_ OK ();
  218. }
  219. Function smtp_error ($ string)
  220. {
  221. $ This-> log_write ("Error: Error occurred while". $ string. ". \ n ");
  222. Return FALSE;
  223. }
  224. Function log_write ($ message)
  225. {
  226. $ This-> smtp_debug ($ message );
  227. If ($ this-> log_file = "")
  228. {
  229. Return TRUE;
  230. }
  231. $ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
  232. If (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, "")))
  233. {
  234. $ This-> smtp_debug ("Warning: Cannot open log file \" ". $ this-> log_file." \ "\ n ");
  235. Return FALSE ;;
  236. }
  237. Flock ($ fp, LOCK_EX );
  238. Fputs ($ fp, $ message );
  239. Fclose ($ fp );
  240. Return TRUE;
  241. }
  242. Function strip_comment ($ address)
  243. {
  244. $ Comment = "\ ([^ ()] * \)";
  245. While (ereg ($ comment, $ address ))
  246. {
  247. $ Address = ereg_replace ($ comment, "", $ address );
  248. }
  249. Return $ address;
  250. }
  251. Function get_address ($ address)
  252. {
  253. $ Address = ereg_replace ("([\ t \ r \ n]) +", "", $ address );
  254. $ Address = ereg_replace ("^. * <(. +)>. * $", "\ 1", $ address );
  255. Return $ address;
  256. }
  257. Function smtp_debug ($ message)
  258. {
  259. If ($ this-> debug)
  260. {
  261. Echo $ message;
  262. }
  263. }
  264. }



Send email, php, smtp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.