PHP email sending

Source: Internet
Author: User
Tags php email email account
PHP email sending

  1. /*************************************** **************************************** ****
  2. Instructions for use:
  3. $ M = new SendM ('smtp server address', 'account', 'password', Port (int), timeout retry time (int ));
  4. $ M-> Send ('recipient mailbox ', 'topic', 'Email body content ');
  5. Example:
  6. $ M = new SendM ('smtp .yeah.net ', 'testuser', 'testuserpwd', 25, 30 );
  7. $ M-> Send ('a @ coolmr.com ', 'Test mail',' this is a test email for mail sending. thank you for your support ');
  8. **************************************** **************************************** *****/
  9. Class SendM {
  10. Private $ Mailhost, $ Mailuser, $ Mailpwd, $ Mailport, $ Mailtimeout, $ ms, $ ending = "\ r \ n", $ endingc = "\ n ";
  11. Function _ construct ($ Mailhost, $ Mailuser, $ Mailpwd, $ Mailport, $ Mailtimeout ){
  12. $ This-> Mailhost = $ Mailhost;
  13. $ This-> Mailuser = $ Mailuser;
  14. $ This-> Mailpwd = $ Mailpwd;
  15. $ This-> Mailport = $ Mailport;
  16. $ This-> Mailtimeout = $ Mailtimeout;
  17. $ This-> ConnectSmtpServer ();
  18. }
  19. Private function ConnectSmtpServer (){
  20. If (! Is_string ($ this-> Mailhost) {settype (trim ($ this-> Mailhost), "string ");}
  21. If (! Is_integer ($ this-> Mailport) {settype (trim ($ this-> Mailport), "integer ");}
  22. If (! Is_integer ($ this-> Mailtimeout) {settype (trim ($ this-> Mailtimeout), "integer ");}
  23. $ This-> ms = @ fsockopen ($ this-> Mailhost, $ this-> Mailport, $ this-> errorno, $ this-> errorstr, $ this-> Mailtimeout );
  24. If (substr (PHP_ OS, 0, 3 )! = "WIN") {stream_set_timeout ($ this-> ms, $ this-> Mailtimeout, 0 );}
  25. $ Rcp = $ this-> get_echo ();
  26. Fputs ($ this-> ms, "ehlo bobo". $ this-> ending );
  27. $ Rcp = $ this-> get_echo ();
  28. If (substr ($ rcp, 0, 3 )! = '20140901') {return false ;}
  29. Fputs ($ this-> ms, 'auth login'. $ this-> ending );
  30. $ Rcp = $ this-> get_echo ();
  31. If (substr ($ rcp, 334) = '000000') {$ this-> Auth ($ this-> Mailuser, $ this-> Mailpwd );} else {return false ;}}
  32. Private function Auth ($ Mailuser, $ Mailpwd ){
  33. $ This-> Mailuseren = base64_encode ($ Mailuser); $ this-> Mailpwden = base64_encode ($ Mailpwd );
  34. Fputs ($ this-> ms, $ this-> Mailuseren. $ this-> ending );
  35. $ Rcp = $ this-> get_echo ();
  36. Fputs ($ this-> ms, $ this-> Mailpwden. $ this-> ending );
  37. $ Rcp = $ this-> get_echo ();}
  38. Private function get_echo (){
  39. $ Edata = ""; while ($ estr = @ fgets ($ this-> ms, 600) {$ edata. = $ estr;
  40. If (substr ($ estr, 3, 1) = "") {break ;}}
  41. Return $ edata ;}
  42. Public function Send ($ to, $ subject, $ connect ){
  43. $ Host = explode ('.', $ this-> Mailhost );
  44. $ Fromaddress = $ this-> Mailuser. '@'. $ host [1]. '.'. $ host [2];
  45. Fputs ($ this-> ms, 'mail from: <'. $ fromaddress.'> '. $ this-> ending );
  46. $ Rcp = $ this-> get_echo ();
  47. Fputs ($ this-> ms, 'rcpt to: <'. $ to.'> '. $ this-> ending );
  48. $ Rcp = $ this-> get_echo ();
  49. Fputs ($ this-> ms, 'data'. $ this-> ending );
  50. $ Rcp = $ this-> get_echo ();
  51. Fputs ($ this-> ms, "to: $ to". $ this-> endingc );
  52. Fputs ($ this-> ms, "from: $ fromaddress". $ this-> endingc );
  53. Fputs ($ this-> ms, "subject: $ subject". $ this-> endingc. $ this-> endingc );
  54. Fputs ($ this-> ms, "$ connect". $ this-> endingc );
  55. Fputs ($ this-> ms, '.'. $ this-> ending );
  56. $ Rcp = $ this-> get_echo (); if (substr ($ rcp, 250) = '000000') {header ("Location: main_pro.php? Act = msg & errors = on & msg = email sent successfully! Submitted to the recipient's server! ");} Else {header (" Location: main_pro.php? Act = msg & errors = on & msg = sorry, the email failed to be sent! Check whether the email account configuration is correct! ");}
  57. }
  58. }
  59. ?>


Send email, 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.