PHP mail operations

Source: Internet
Author: User
Tags ereg
PHP mail operations

  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 ($ ));
  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, ""))){
  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 server
  271. $ Smtpserverport = 25; // SMTP server port
  272. $ Smtpusermail = "caowlong163@163.com"; // user email address of the SMTP server
  273. $ Smtpemailto = "caowlong@qq.com"; // to whom to send
  274. $ Smtpuser = "caowlong163@163.com"; // User account of the SMTP server
  275. $ Smtppass = "XXX"; // SMTP server user password
  276. $ Mailsubject = "PHP100 test mail system"; // mail subject
  277. $ Mailbody = "your username is Michael and password is 11111"; // email content
  278. $ Mailtype = "HTML"; // The email format (HTML/TXT). TXT is a text email.
  279. $ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass );
  280. $ Smtp-> debug = true; // whether to display the sent debugging information
  281. $ Smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mailtype );
  282. ?>


PHP

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.