How to configure smtp mail sending in php in linux

Source: Internet
Author: User
How to configure smtp mail sending in php in linux

  1. Include_once ("class. phpmailer. php ");

  2. /**
  3. * Define email module preparation information
  4. */
  5. Define ("SMTP_HOST", "smtp.mail.yahoo.com"); // SMTP host
  6. Define ("SMTP_MAIL", "XXXX@yahoo.cn"); // SMTP user email
  7. Define ("SMTP_PASS", "XXXX"); // SMTP password

  8. Define ("SERVICE_MAIL", "XXXX@yahoo.cn"); // SMTP user email

  9. Define ("SERVICE_NAME", "PHPBOOK mail Test"); // SMTP name

  10. /**

  11. * Use the phpmailer mail module
  12. *
  13. * @ Param string $ email
  14. * @ Param string $ user
  15. * @ Param string $ subject
  16. * @ Param string $ body
  17. * @ Return bool
  18. */
  19. Function sendMail ($ email, $ user, $ subject, $ body)
  20. {
  21. $ Mail = new PHPMailer ();
  22. // $ This;
  23. $ Mail-> IsSMTP (); // you can specify whether to use SMTP.
  24. $ Mail-> Host = SMTP_HOST; // you can specify the SMTP server address.
  25. $ Mail-> SMTPAuth = true; // enable SMTP permission verification
  26. $ Mail-> Username = SMTP_MAIL; // SMTP Username
  27. $ Mail-> Password = SMTP_PASS; // SMTP server Password

  28. $ Mail-> From = SERVICE_MAIL; // you can specify the sender address.

  29. $ Mail-> FromName = SERVICE_NAME; // you can specify the sender name.
  30. $ Mail-> AddAddress ($ email, $ user); // add the recipient address
  31. $ Mail-> AddReplyTo (SERVICE_MAIL, SERVICE_NAME); // you can specify a reply address.

  32. $ Mail-> WordWrap = 50; // Set the display format

  33. $ Mail-> IsHTML (true); // you can specify html for an email.
  34. $ Mail-> Subject = $ subject;
  35. $ Mail-> Body = $ body;
  36. $ Mail-> AltBody = ""; // text-type email

  37. If (! $ Mail-> Send ())

  38. {
  39. Return $ mail-> ErrorInfo;
  40. }
  41. Return true;
  42. }

  43. // Start sending the test email ng: fsockopen () [function. fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in/var/www/xiehui/admin/mail/class. smtp. php on line 89

  44. $ Tomail = "XXXX@126.com ";
  45. $ User = "XXXXlinux ";
  46. $ _ MailSubject = "email test example! "; // The Mail title group sent to the user
  47. $ _ MailBody = "xinlang net"; // email content group
  48. SendMail ($ tomail, $ user, $ _ mailSubject, $ _ mailBody );
  49. ?>

Experiment proves that yahoo's smtp is very easy to use, and sina is actually not easy to use. I am stuck for a long time.

Method 4To the encapsulation class for the program written by the socket to send emails using the socket:

  1. Class sendmail {
  2. Var $ lastmessage; // record the last response
  3. Var $ lastact; // The final action, in the string format
  4. Var $ welcome; // used after HELO. welcome
  5. Var $ debug; // whether to display debugging information
  6. Var $ smtp; // smtp server
  7. Var $ port; // smtp port number
  8. Var $ fp; // socket handle
  9. // Email sending function
  10. Function send_mail ($ smtp, $ welcome = "", $ debug = false ){
  11. If (empty ($ smtp) die ("SMTP cannot be blank! ");
  12. $ This-> smtp = $ smtp;
  13. If (empty ($ welcome )){
  14. $ This-> welcome = gethostbyaddr ("localhost ");
  15. } Else
  16. $ This-> welcome = $ welcome;
  17. $ This-> debug = $ debug;
  18. $ This-> lastmessage = "";
  19. $ This-> lastact = "";
  20. $ This-> port = "25 ";
  21. }
  22. // Display debugging information
  23. Function show_debug ($ message, $ inout ){
  24. If ($ this-> debug ){
  25. If ($ inout = "in") {// response information
  26. $ M = '<';
  27. } Else
  28. $ M = '> ';
  29. If (! Ereg ("\ n $", $ message ))
  30. $ Message. ="
    ";
  31. $ Message = nl2br ($ message );
  32. Echo "$ {m }$ {message }";
  33. }
  34. }
  35. // Execute the passed command
  36. Function do_command ($ command, $ code ){
  37. $ This-> lastact = $ command;
  38. $ This-> show_debug ($ this-> lastact, "out ");
  39. Fputs ($ this-> fp, $ this-> lastact );
  40. $ This-> lastmessage = fgets ($ this-> fp, 512 );
  41. $ This-> show_debug ($ this-> lastmessage, "in ");
  42. If (! Ereg ("^ $ code", $ this-> lastmessage ))
  43. Return false;
  44. Else
  45. Return true;
  46. }
  47. // Mail handling
  48. Function send ($ to, $ from, $ subject, $ message ){
  49. // Connect to the server
  50. $ This-> lastact = "connect ";
  51. $ This-> show_debug ("connect to SMTP server:". $ this-> smtp, "out ");
  52. $ This-> fp = fsockopen ($ this-> smtp, $ this-> port );
  53. If ($ this-> fp ){
  54. $ This-> set_socket_blocking ($ this-> fp, true );
  55. $ This-> lastmessage = fgets ($ this-> fp, 512 );
  56. $ This-> show_debug ($ this-> lastmessage, "in ");
  57. If (! Ereg ("^ 220", $ this-> lastmessage )){
  58. Return false;
  59. } Else {
  60. $ This-> lastact = "HELO". $ this-> welcome. "\ n ";
  61. If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
  62. Fclose ($ this-> fp );
  63. Return false;
  64. }
  65. $ This-> lastact = "mail from: $ from". "\ n ";
  66. If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
  67. Fclose ($ this-> fp );
  68. Return false;
  69. }
  70. $ This-> lastact = "rcpt to: $ to". "\ n ";
  71. If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
  72. Fclose ($ this-> fp );
  73. Return false;
  74. }
  75. // Start sending the email body
  76. $ This-> lastact = "DATA \ n ";
  77. If (! $ This-& gt; do_command ($ this-& gt; lastact, "354 ")){
  78. Fclose ($ this-> fp );
  79. Return false;
  80. }
  81. // Start to process the subject header
  82. $ Head = "Subject: $ subject \ n ";
  83. If (! Empty ($ subject )&&! Ereg ($ head, $ message )){
  84. $ Message = $ head. $ message;
  85. }
  86. // Start to process the mail From header
  87. $ Head = "From: $ from \ n ";
  88. If (! Empty ($ from )&&! Ereg ($ head, $ message )){
  89. $ Message = $ head. $ message;
  90. }
  91. // Start To process the mail To header
  92. $ Head = "To: $ to \ n ";
  93. If (! Empty ($ )&&! Ereg ($ head, $ message )){
  94. $ Message = $ head. $ message;
  95. }
  96. // Process the end string
  97. If (! Ereg ("\ n \. \ n", $ message ))
  98. $ Message. = "\ n. \ n ";
  99. $ This-> show_debug ($ message, "out ");
  100. Fputs ($ this-> fp, $ message );
  101. $ This-> lastact = "QUIT \ n ";
  102. If (! $ This-& gt; do_command ($ this-& gt; lastact, "250 ")){
  103. Fclose ($ this-> fp );
  104. Return false;
  105. }
  106. }
  107. Return true;
  108. } Else {
  109. $ This-> show_debug ("connection failed !! "," In ");
  110. Return false;
  111. }
  112. }
  113. }
  114. ?>

Example of sending an email using socket:

  1. Include ("./sendmail. class. php ");
  2. $ Mail = new sendmail ();
  3. $ Email = "Hello, this is a test email! ";
  4. $ Sendmail = new send_mail ("smtp.mail.126.com", "PHPBOOK", true); // Display The Call Information
  5. If ($ mail-> send ("XXXX@126.com", "XXXX@126.com", "test SOCKET mail", $ email )){
  6. Echo "sent successfully!
    ";
  7. } Else {
  8. Echo "failed to send!
    ";
  9. }
  10. ?>

Related Article

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.