Phpsocket uses smtp to send emails with attachments

Source: Internet
Author: User
Tags eol
Phpsocket uses smtp to send emails with attachments

  1. /**

  2. * Php socket smtp send mail
  3. * Edit: bbs.it-home.org
  4. */

  5. // Define ("SOL", "\ n ");

  6. Define ("EOL", "\ r \ n ");
  7. Define ("SMTP_HOST", "smtp.163.com"); // SMTP server
  8. Define ("SMTP_PORT", "25"); // SMTP server port
  9. Define ("SMTP_USER", ""); // User account of the SMTP server
  10. Define ("SMTP_PASS", ""); // SMTP server user password

  11. $ From = ""; // user email address of the SMTP server

  12. $ To = ""; // The sender can use commas to separate multiple mailboxes.
  13. $ Cc = "";
  14. $ Bcc = "";

  15. $ Subject = "this is an email with attachments sent by PHP"; // The subject of the email may contain garbled characters on many clients.

  16. $ Body = "this is an email sending program with attachments. The HTM label is displayed. click the link.". Date ('Y-m-d H: I: s'); // mail content
  17. $ Smtp = new smtp (SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS, true); // Here, true indicates that authentication is used; otherwise, authentication is not used.
  18. $ Smtp-> addAttachment ("mail.zip ");
  19. $ Smtp-> sendmail ($ to, $ from, $ subject, $ body, $ cc, $ bcc );

  20. Class smtp {

  21. /* Public Variables */

  22. Public $ attachments = array ();
  23. /* Private Variables */
  24. Private $ smtp_host;
  25. Private $ smtp_port;
  26. Private $ time_out;
  27. Private $ host_name;
  28. Private $ auth;
  29. Private $ user;
  30. Private $ pass;
  31. Private $ sock;

  32. /* Constractor */

  33. Public function smtp ($ smtp_host = null, $ smtp_port = null, $ user = null, $ pass = null, $ auth = true ){
  34. $ This-> smtp_host = (! Empty ($ smtp_host ))? $ Smtp_host: SMTP_HOST;
  35. $ This-> smtp_port = (! Empty ($ smtp_port ))? $ Smtp_port: SMTP_PORT;
  36. $ This-> user = (! Empty ($ user ))? $ User: SMTP_PORT;
  37. $ This-> pass = (! Empty ($ pass ))? $ Pass: SMTP_PORT;
  38. $ This-> auth = $ auth;
  39. $ This-> time_out = 15;
  40. #
  41. $ This-> host_name = "localhost ";
  42. $ This-> sock = FALSE;
  43. }

  44. /* Main Function */

  45. Public function sendmail ($ to, $ from, $ subject = "", $ body = "", $ cc = "", $ bcc = ""){
  46. $ Bndp = md5 (uniqid (""). rand (1000,999 9 );
  47. $ Bnd = md5 (uniqid (""). rand (1000,999 9 );
  48. List ($ msec, $ sec) = explode ("", microtime ());

  49. $ Mail_from = $ this-> strip_line_breaks ($ from );

  50. $ Mail_to = explode (",", $ );
  51. $ Body = preg_replace ("/(^ | (\ r \ n) (\.)/", "", $ body );
  52. If ($ cc! = "") $ Mail_to = array_merge ($ mail_to, explode (",", $ cc ));
  53. If ($ bcc! = "") $ Mail_to = array_merge ($ mail_to, explode (",", $ bcc ));

  54. $ Headers = "maid: 1.0". EOL;

  55. $ Headers. = "To:". $ to. EOL;
  56. If ($ cc! = ""){
  57. $ Headers. = "Cc:". $ cc. EOL;
  58. }
  59. $ Headers. = "From: $ from <". $ from. ">". EOL;
  60. $ Headers. = "Subject:". $ subject. EOL;
  61. $ Headers. = "Date:". date ("r"). EOL;
  62. $ Headers. = "X-Mailer: Webmail ver 1.0 (PHP Version/". phpversion (). ")". EOL;
  63. $ Headers. = "Message-ID: <". date ("YmdHis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ from. "> ". EOL;
  64. If (count ($ this-> attachments)> 0 ){
  65. $ Headers. = "Content-Type: multipart/mixed ;". EOL. chr (9 ). "boundary = \"". $ bndp. "\"". EOL. EOL;
  66. $ Headers. = '--'. $ bndp. EOL;
  67. $ Headers. = 'content-Type: multipart/alternative; boundary = "'. $ bnd.'" '. EOL. EOL;
  68. $ Headers. = '--'. $ bnd. EOL;
  69. $ Headers. = 'content-Type: text/plain; charset = utf-8 '. EOL;
  70. $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
  71. $ Headers. = $ body. EOL;
  72. $ Headers. = '--'. $ bnd. EOL;
  73. $ Headers. = 'content-type: text/html; charset = utf-8 '. EOL;
  74. $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
  75. $ Headers. = $ body. EOL;
  76. $ Headers. = '--'. $ bnd. '--'. EOL;

  77. Foreach ($ this-> attachments as $ att ){

  78. $ Headers. = "--". $ bndp. EOL. $ att;
  79. }
  80. $ Headers. = '--'. $ bndp. '--'. EOL;
  81. $ This-> clear_attachments ();
  82. } Else {
  83. $ Headers. = 'content-Type: multipart/alternative; boundary = "'. $ bnd.'" '. EOL. EOL;
  84. $ Headers. = '--'. $ bnd. EOL;
  85. $ Headers. = 'content-Type: text/plain; charset = utf-8 '. EOL;
  86. $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
  87. $ Headers. = $ body. EOL;
  88. $ Headers. = '--'. $ bnd. EOL;
  89. $ Headers. = 'content-type: text/html; charset = utf-8 '. EOL;
  90. $ Headers. = "Content-Transfer-Encoding: 8bit". EOL. EOL;
  91. $ Headers. = $ body. EOL;
  92. $ Headers. = '--'. $ bnd. '--'. EOL;
  93. }

  94. $ Sent = TRUE;

  95. Foreach ($ mail_to as $ rcpt_to ){
  96. $ Rcpt_to = $ this-> strip_line_breaks ($ rcpt_to );
  97. If (! $ This-> smtp_sockopen ($ rcpt_to )){
  98. $ This-> log_write ("Error: Cannot send email to". $ rcpt_to );
  99. $ Sent = FALSE;
  100. Continue;
  101. }
  102. If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ headers, $ body )){
  103. $ This-> log_write ("E-mail has been sent to <". $ rcpt_to. "> ");
  104. } Else {
  105. $ This-> log_write ("Error: Cannot send email to <". $ rcpt_to. "> ");
  106. $ Sent = FALSE;
  107. }
  108. Fclose ($ this-> sock );
  109. }
  110. $ This-> log_write ("{$ mail_to} send over ;");
  111. Return $ sent;
  112. }

  113. Public function addAttachment ($ file, $ dispo = "attachment "){

  114. $ File_data = (file_exists ($ file ))? File_get_contents ($ file ):"";
  115. If ($ file_data! = ""){
  116. $ Filename = basename ($ file );
  117. $ Ext = pathinfo ($ filename, PATHINFO_EXTENSION );
  118. $ Chunks = chunk_split (base64_encode ($ file_data ));
  119. $ Parts = "Content-Type: application/$ ext; name = \" ". $ filename." \ "". EOL;
  120. $ Parts. = "Content-Transfer-Encoding: base64". EOL;
  121. $ Parts. = "Content-Disposition:". $ dispo. "; filename = \" ". $ filename." \ "". EOL. EOL;
  122. $ Parts. = $ chunks. EOL. EOL;
  123. $ This-> attachments [] = $ parts;
  124. }
  125. }

  126. Private function clear_attachments (){

  127. Unset ($ this-> attachments );
  128. $ This-> attachments = array ();
  129. }

  130. /* Private Functions */

  131. Private function smtp_send ($ helo, $ from, $ to, $ header, $ body = ""){
  132. If (! $ This-> smtp_putcmd ("HELO", $ helo )){
  133. // $ This-> log_write ("Error: Error occurred while sending HELO command .");
  134. Return FALSE;
  135. }
  136. # Auth
  137. If ($ this-> auth ){
  138. If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
  139. // $ This-> log_write ("Error: Error occurred while sending HELO command .");
  140. Return FALSE;
  141. }
  142. If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
  143. // $ This-> log_write ("Error: Error occurred while sending HELO command .");
  144. Return FALSE;
  145. }
  146. }
  147. If (! $ This-> smtp_putcmd ("MAIL", "FROM: <". $ from. "> ")){
  148. // $ This-> log_write ("Error: Error occurred while sending mail from command .");
  149. Return FALSE;
  150. }
  151. If (! $ This-> smtp_putcmd ("RCPT", "TO: <". $ to. "> ")){
  152. // $ This-> log_write ("Error: Error occurred while sending rcpt to command .");
  153. Return FALSE;
  154. }
  155. If (! $ This-> smtp_putcmd ("DATA ")){
  156. // $ This-> log_write ("Error: Error occurred while sending DATA command .");
  157. Return FALSE;
  158. }
  159. If (! $ This-> smtp_message ($ header, $ body )){
  160. // $ This-> log_write ("Error: Error occurred while sending message .");
  161. Return FALSE;
  162. }
  163. If (! $ This-> smtp_eom ()){
  164. // $ This-> log_write ("Error: Error occurred while sending . [EOM]. ");
  165. Return FALSE;
  166. }
  167. If (! $ This-> smtp_putcmd ("QUIT ")){
  168. // $ This-> log_write ("Error: Error occurred while sending QUIT command .");
  169. Return FALSE;
  170. }
  171. Return TRUE;
  172. }
  173. Private function smtp_sockopen ($ address ){
  174. If ($ this-> smtp_host = ""){
  175. Return $ this-> smtp_sockopen_mx ($ address );
  176. } Else {// bbs.it-home.org
  177. Return $ this-> smtp_sockopen_relay ();
  178. }
  179. }
  180. Private function smtp_sockopen_relay (){
  181. $ This-> log_write ("Trying to Connect". $ this-> smtp_host. ":". $ this-> smtp_port ."...");
  182. $ This-> sock = @ fsockopen ($ this-> smtp_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  183. If (! ($ This-> sock & $ this-> smtp_ OK ())){
  184. $ This-> log_write ("Error: connenct error". $ errstr. "(". $ errno .")");
  185. Return FALSE;
  186. }
  187. $ This-> log_write ("Connected OK ");
  188. Return TRUE;
  189. }
  190. Private function smtp_sockopen_mx ($ address ){
  191. $ Domain = preg_replace ("/^. + @ ([^ @] +) $/", "\ 1", $ address );
  192. If (! @ Getmxrr ($ domain, $ MXHOSTS )){
  193. $ This-> log_write ("Error: Cannot resolve MX \" ". $ domain ."\"");
  194. Return FALSE;
  195. }
  196. Foreach ($ MXHOSTS as $ host ){
  197. $ This-> log_write ("Trying to". $ host. ":". $ this-> smtp_port );
  198. $ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
  199. If (! ($ This-> sock & $ this-> smtp_ OK ())){
  200. $ This-> log_write ("Connect Error,". $ errstr. "(". $ errno .")");
  201. Continue;
  202. }
  203. $ This-> log_write ("Connected to mx host". $ host );
  204. Return TRUE;
  205. }
  206. $ This-> log_write ("Error: Cannot connect to any mx hosts (". implode (",", $ MXHOSTS ).")");
  207. Return FALSE;
  208. }
  209. Private function smtp_message ($ header, $ body ){
  210. Fputs ($ this-> sock, $ header. "\ r \ n". $ body );
  211. Return TRUE;
  212. }
  213. Private function smtp_eom (){
  214. Fputs ($ this-> sock, "\ r \ n. \ r \ n ");
  215. Return $ this-> smtp_ OK ();
  216. }
  217. Private function smtp_ OK (){
  218. $ Response = str_replace ("\ r \ n", "", fgets ($ this-> sock, 512 ));
  219. If (! Preg_match ("/^ [23]/", $ response )){
  220. Fputs ($ this-> sock, "QUIT \ r \ n ");
  221. Fgets ($ this-> sock, 512 );
  222. $ This-> log_write ("Error: Remote host returned \" ". $ response ."\"");
  223. Return FALSE;
  224. }
  225. Return TRUE;
  226. }
  227. Private function smtp_putcmd ($ cmd, $ arg = ""){
  228. If ($ arg! = "") $ Cmd = ($ cmd = "")? $ Arg: ($ cmd. "". $ arg );
  229. Fputs ($ this-> sock, $ cmd. "\ r \ n ");
  230. Return $ this-> smtp_ OK ();
  231. }
  232. Private function strip_line_breaks ($ address ){
  233. $ Address = preg_replace ("/([\ t \ r \ n]) +/", "", $ address );
  234. $ Address = preg_replace ("/^. * <(. +)>. * $/", "", $ address );
  235. Return $ address;
  236. }
  237. Public function log_write ($ message ){
  238. $ Message = date ("M d H: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
  239. File_put_contents (dirname (_ FILE _). '/mail. log', $ message. PHP_EOL, FILE_APPEND | LOCK_EX );
  240. }
  241. }

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.