Smtp mail sending class written in php

Source: Internet
Author: User
Tags ereg
Smtp mail sending class written in php

  1. $ Smtpserver = "*****";
  2. $ Smtpserverport = 25;
  3. $ Smtpuser = "******";
  4. $ Smtppass = "*******";
  5. $ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // Here, true indicates that authentication is used; otherwise, authentication is not used.
  6. $ Smtp-> debug = false;
  7. // $ Emailtype = "HTML ";
  8. For ($ I = 0; $ I <5; $ I ++ ){
  9. $ Smtp-> sendmail ("*****", "*******", "Hello world! "," This is only a test! ");
  10. }
  11. Echo "sent in total$ IEmail! ";
  12. ?>

The following describes the implementation of a specific class.

  1. Class smtp {

  2. /* Public Variables */
  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. /* Private Variables */
  13. Var $ sock;
  14. /* Constractor */
  15. Function smtp ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass)
  16. {
  17. $ This-> debug = false;
  18. $ This-> smtp_port = $ smtp_port;
  19. $ This-> relay_host = $ relay_host;
  20. $ This-> time_out = 30; // is used in fsockopen ()
  21. $ This-> auth = $ auth; // auth
  22. $ This-> user = $ user;
  23. $ This-> pass = $ pass;
  24. $ This-> host_name = "localhost"; // is used in HELO command
  25. $ This-> log_file = "";
  26. $ This-> sock = false;
  27. }
  28. /* Main Function */
  29. Function sendmail ($ to, $ from, $ subject = "", $ body = "", $ mailtype = "", $ cc = "", $ bcc = "", $ additional_headers = "")
  30. {
  31. $ Mail_from = $ this-> get_address ($ this-> strip_comment ($ from ));
  32. $ Body = ereg_replace ("(^ | () (.)", "1.3", $ body );
  33. $ Header. = "maid: 1.0 ";
  34. If ($ mailtype = "HTML "){
  35. $ Header. = "Content-Type: text/html ";
  36. }
  37. $ Header. = "To:". $ ."";
  38. If ($ cc! = ""){
  39. $ Header. = "Cc:". $ cc ."";
  40. }
  41. $ Header. = "From: $ from <". $ from. "> ;";
  42. $ Header. = "Subject:". $ subject ."";
  43. $ Header. = $ additional_headers;
  44. $ Header. = "Date:". date ("r ")."";
  45. $ Header. = "X-Mailer: By Redhat (PHP/". phpversion ().")";
  46. List ($ msec, $ sec) = explode ("", microtime ());
  47. $ Header. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> ;";
  48. $ TO = explode (",", $ this-> strip_comment ($ ));
  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 ."");
  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. "> ;");
  65. } Else {
  66. $ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> ;");
  67. $ Sent = false;
  68. }
  69. Fclose ($ this-> sock );
  70. $ This-> log_write ("Disconnected from remote host ");
  71. }
  72. Return $ sent;
  73. }

  74. /* Private Functions */

  75. Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
  76. {
  77. If (! $ This-> smtp_putcmd ("HELO", $ helo )){
  78. Return $ this-> smtp_error ("sending HELO command ");
  79. }
  80. // Auth
  81. If ($ this-> auth ){
  82. If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
  83. Return $ this-> smtp_error ("sending HELO command ");
  84. }
  85. If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
  86. Return $ this-> smtp_error ("sending HELO command ");
  87. }
  88. }
  89. If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ;")){
  90. Return $ this-> smtp_error ("sending mail from command ");
  91. }
  92. If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ;")){
  93. Return $ this-> smtp_error ("sending rcpt to command ");
  94. }
  95. If (! $ This-> smtp_putcmd ("DATA ")){
  96. Return $ this-> smtp_error ("sending DATA command ");
  97. }
  98. If (! $ This-> smtp_message ($ header, $ body )){
  99. Return $ this-> smtp_error ("sending message ");
  100. }
  101. If (! $ This-> smtp_eom ()){
  102. Return $ this-> smtp_error ("sending ; ;. ; ; [EOM] ");
  103. }
  104. If (! $ This-> smtp_putcmd ("QUIT ")){
  105. Return $ this-> smtp_error ("sending QUIT command ");
  106. }
  107. Return true;
  108. }
  109. Function smtp_sockopen ($ address)
  110. {
  111. If ($ this-> relay_host = ""){
  112. Return $ this-> smtp_sockopen_mx ($ address );
  113. } Else {
  114. Return $ this-> smtp_sockopen_relay ();
  115. }
  116. }
  117. Function smtp_sockopen_relay ()
  118. {
  119. $ This-> log_write ("Trying to". $ this-> relay_host. ":". $ this-> smtp_port ."");
  120. $ This-> sock = @ fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  121. If (! ($ This-> sock & $ this-> smtp_ OK ())){
  122. $ This-> log_write ("Error: Cannot connenct to relay host". $ this-> relay_host ."");
  123. $ This-> log_write ("Error:". $ errstr. "(". $ errno .")");
  124. Return false;
  125. }
  126. $ This-> log_write ("Connected to relay host". $ this-> relay_host ."");
  127. Return true ;;
  128. }
  129. Function smtp_sockopen_mx ($ address)
  130. {
  131. $ Domain = ereg_replace ("^. + @ ([^ @] +) $", "1", $ address );
  132. If (! @ Getmxrr ($ domain, $ MXHOSTS )){
  133. $ This-> log_write ("Error: Cannot resolve MX" ". $ domain .""");
  134. Return false;
  135. }
  136. Foreach ($ MXHOSTS as $ host ){
  137. $ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port ."");
  138. $ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  139. If (! ($ This-> sock & $ this-> smtp_ OK ())){
  140. $ This-> log_write ("Warning: Cannot connect to mx host". $ host ."");
  141. $ This-> log_write ("Error:". $ errstr. "(". $ errno .")");
  142. Continue;
  143. }
  144. $ This-> log_write ("Connected to mx host". $ host ."");
  145. Return true;
  146. }
  147. $ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS ).")");
  148. Return false;
  149. }
  150. Function smtp_message ($ header, $ body)
  151. {
  152. Fputs ($ this-> sock, $ header. "". $ body );
  153. $ This-> smtp_debug ("> ;". str_replace ("","". ">;", $ header. "> ;". $ body. "> ;"));
  154. Return true;
  155. }
  156. Function smtp_eom ()
  157. {
  158. Fputs ($ this-> sock ,".");
  159. $ This-> smtp_debug (". [EOM]");
  160. Return $ this-> smtp_ OK ();
  161. }
  162. Function smtp_ OK ()
  163. {
  164. $ Response = str_replace ("", "", fgets ($ this-> sock, 512 ));
  165. $ This-> smtp_debug ($ response ."");
  166. If (! Ereg ("^ [23]", $ response )){
  167. Fputs ($ this-> sock, "QUIT ");
  168. Fgets ($ this-> sock, 512 );
  169. $ This-> log_write ("Error: Remote host returned" ". $ response .""");
  170. Return false;
  171. }
  172. Return true;
  173. }
  174. Function smtp_putcmd ($ cmd, $ arg = "")
  175. {
  176. If ($ arg! = ""){
  177. If ($ cmd = "") $ cmd = $ arg;
  178. Else $ cmd = $ cmd. "". $ arg;
  179. }
  180. Fputs ($ this-> sock, $ cmd ."");
  181. $ This-> smtp_debug (">;". $ cmd ."");
  182. Return $ this-> smtp_ OK ();
  183. }
  184. Function smtp_error ($ string)
  185. {
  186. $ This-> log_write ("Error: Error occurred while". $ string .".");
  187. Return false;
  188. }
  189. Function log_write ($ message)
  190. {
  191. $ This-> smtp_debug ($ message );
  192. If ($ this-> log_file = ""){
  193. Return true;
  194. }
  195. $ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
  196. If (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, ""))){
  197. $ This-> smtp_debug ("Warning: Cannot open log file" ". $ this-> log_file .""");
  198. Return false ;;
  199. }
  200. Flock ($ fp, LOCK_EX );
  201. Fputs ($ fp, $ message );
  202. Fclose ($ fp );
  203. Return true;
  204. }
  205. Function strip_comment ($ address)
  206. {
  207. $ Comment = "([^ ()] *)";
  208. While (ereg ($ comment, $ address )){
  209. $ Address = ereg_replace ($ comment, "", $ address );
  210. }
  211. Return $ address;
  212. }
  213. Function get_address ($ address)
  214. {
  215. $ Address = ereg_replace ("([]) +", "", $ address );
  216. $ Address = ereg_replace ("^. * <(. +)>;. * $", "1", $ address );
  217. Return $ address;
  218. }
  219. Function smtp_debug ($ message)
  220. {
  221. If ($ this-> debug ){
  222. Echo $ message .";";
  223. }
  224. }
  225. }
  226. ?>

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.