How to configure SMTP to send mail under Linux

Source: Internet
Author: User
Tags ereg
  1. Include_once ("class.phpmailer.php");

  2. /**
  3. * Define Message Module configuration 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"); The password used for SMTP

  8. Define ("Service_mail", "XXXX@yahoo.cn"); SMTP User Email

  9. Define ("Service_Name", "Phpbook Mail Test"); The name used for SMTP

  10. /**

  11. * Use Phpmailer Email 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 (); Set up to use SMTP
  24. $mail->host = smtp_host; Set the SMTP server address
  25. $mail->smtpauth = true; Turn on SMTP permission validation
  26. $mail->username = Smtp_mail; SMTP User Name
  27. $mail->password = Smtp_pass; SMTP Server Password

  28. $mail->from = Service_mail; Set Sender Address

  29. $mail->fromname = service_name; Set Sender Name
  30. $mail->addaddress ($email, $user); Add recipient Address
  31. $mail->addreplyto (Service_mail, service_name); Set Reply Address

  32. $mail->wordwrap = 50; Set display format

  33. $mail->ishtml (TRUE); Set up mail support HTML
  34. $mail->subject = $subject;
  35. $mail->body = $body;
  36. $mail->altbody = ""; Text type of message

  37. if (! $mail->send ())

  38. {
  39. return $mail->errorinfo;
  40. }
  41. return true;
  42. }

  43. Start sending test message Ng:fsockopen () [Function.fsockopen]: php_network_getaddresses:getaddrinfo failed:name or service not known I N/var/www/xiehui/admin/mail/class.smtp.php on line 89

  44. $tomail = "XXXX@126.com";
  45. $user = "Xxxxlinux";
  46. $_mailsubject = "Mail test sample!"; Message header Group to users
  47. $_mailbody = "Sina net"; Mail Content Group
  48. SendMail ($tomail, $user, $_mailsubject,$_mailbody);
  49. ?>

Copy Code

The experiment proves that the SMTP of Yahoo is very easy to use, so-called Sina actually does not use, I stuck in a good long time.

method Four , give the socket written by the program to use the socket to send mail package class:

  1. Class sendmail{
  2. var $lastmessage; Record the last returned response information
  3. var $lastact; The last action, the string form
  4. var $welcome; Use behind helo, welcome user
  5. var $debug; Whether to display debug information
  6. var $smtp; SMTP server
  7. var $port; SMTP port number
  8. var $fp; Socket handle
  9. Send mail function
  10. function Send_mail ($smtp, $welcome = "", $debug =false) {
  11. if (empty ($SMTP)) Die ("SMTP cannot be empty!") ");
  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 Debug 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 delivery processing
  48. function Send ($to, $from, $subject, $message) {
  49. Connecting to a 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->do_command ($this->lastact, "250")) {
  62. Fclose ($this->FP);
  63. return false;
  64. }
  65. $this->lastact= "MAIL from: $from". "\ n";
  66. if (! $this->do_command ($this->lastact, "250")) {
  67. Fclose ($this->FP);
  68. return false;
  69. }
  70. $this->lastact= "RCPT to: $to". "\ n";
  71. if (! $this->do_command ($this->lastact, "250")) {
  72. Fclose ($this->FP);
  73. return false;
  74. }
  75. Start sending message body
  76. $this->lastact= "data\n";
  77. if (! $this->do_command ($this->lastact, "354")) {
  78. Fclose ($this->FP);
  79. return false;
  80. }
  81. Start Processing Message topic header
  82. $head = "Subject: $subject \ n";
  83. if (!empty ($subject) &&!ereg ($head, $message)) {
  84. $message = $head. $message;
  85. }
  86. Start processing messages from header
  87. $head = "From: $from \ n";
  88. if (!empty ($from) &&!ereg ($head, $message)) {
  89. $message = $head. $message;
  90. }
  91. Start processing mail to Header
  92. $head = "to: $to \ n";
  93. if (!empty ($to) &&!ereg ($head, $message)) {
  94. $message = $head. $message;
  95. }
  96. Processing 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->do_command ($this->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. ?>
Copy Code

Example of sending a message using the socket:

    1. Include ("./sendmail.class.php");
    2. $mail = new SendMail ();
    3. $email = "Hello, this is a test mail!" ";
    4. $sendmail = new Send_mail ("smtp.mail.126.com", "Phpbook", true); Display information
    5. if ($mail->send ("XXXX@126.com", "XXXX@126.com", "Test socket Mail", $email)) {
    6. echo "sent successfully!
      ";
    7. }else{
    8. echo "Send failed!
      ";
    9. }
    10. ?>
Copy Code
  • 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.