Home > Backend Development > PHP Tutorial > How to configure smtp to send emails in php under linux

How to configure smtp to send emails in php under linux

WBOY
Release: 2016-07-25 08:54:43
Original
1637 people have browsed it
  1. include_once("class.phpmailer.php");
  2. /**
  3. * Define email 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"); // Password for SMTP
  8. define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP user email

  9. define("SERVICE_NAME","PHPBOOK Email Test"); // SMTP name used
  10. /**

  11. * Use phpmailer to send 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 SMTP
  24. $mail->Host = SMTP_HOST; // Set SMTP server address
  25. $mail->SMTPAuth = true; // Turn on SMTP permission verification
  26. $mail-> Username = SMTP_MAIL; // SMTP username
  27. $mail->Password = SMTP_PASS; // SMTP server password

  28. $mail->From = SERVICE_MAIL; // Set sender address

  29. $mail->FromName = SERVICE_NAME; // Set the sender's name
  30. $mail->AddAddress($email, $user); // Add the recipient's address
  31. $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); / / Set reply address

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

  33. $mail->IsHTML(true); // Set email to support html
  34. $mail-> ;Subject = $subject;
  35. $mail->Body = $body;
  36. $mail->AltBody = ""; // Text type mail

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

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

  43. //Start sending test email: 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!"; // Email subject group sent to the user
  47. $_mailBody = "Sina"; // Mail content team
  48. sendMail($tomail,$user,$_mailSubject,$_mailBody);
  49. ?>

Copy code

Experiments have proven that Yahoo's SMTP is very easy to use, but the so-called Sina one is actually not easy to use. I was stuck for a long time.

Method 4, given to the program written by socket Encapsulation class for sending emails using socket:

  1. class sendmail{
  2. var $lastmessage; //Record the last response message returned
  3. var $lastact; //Last action in string form
  4. var $welcome; //Used in HELO Later, users are 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. //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. //显示调试信息
  23. function show_debug($message, $inout) {
  24. if ($this->debug) {
  25. if($inout=="in"){ //响应信息
  26. $m='<< ';
  27. }else
  28. $m='>> ';
  29. if(!ereg("n$", $message))
  30. $message .= "
    ";
  31. $message=nl2br($message);
  32. echo "${m}${message}";
  33. }
  34. }
  35. //执行传递的命令
  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. //邮件发送处理
  48. function send( $to,$from,$subject,$message) {
  49. //连接服务器
  50. $this->lastact="connect";
  51. $this->show_debug("连接到SMTP 服务器: ".$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. //开始发送邮件正文
  76. $this->lastact="DATAn";
  77. if(!$this->do_command($this->lastact, "354")){
  78. fclose($this->fp);
  79. return false;
  80. }
  81. //开始处理邮件主题头
  82. $head="Subject: $subjectn";
  83. if(!empty($subject) && !ereg($head, $message)){
  84. $message = $head.$message;
  85. }
  86. //开始处理邮件From头
  87. $head="From: $fromn";
  88. if(!empty($from) && !ereg($head, $message)) {
  89. $message = $head.$message;
  90. }
  91. //开始处理邮件To头
  92. $head="To: $ton";
  93. if(!empty($to) && !ereg($head, $message)) {
  94. $message = $head.$message;
  95. }
  96. //处理结束串
  97. if(!ereg("n.n", $message))
  98. $message .= "n.n";
  99. $this->show_debug($message, "out");
  100. fputs($this->fp, $message);
  101. $this->lastact="QUITn";
  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("连接失败!!", "in");
  110. return false;
  111. }
  112. }
  113. }
  114. ?>
复制代码

Example of sending 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 adjustment information
  5. if($mail->send("XXXX@126.com", "XXXX@ 126.com", "Test SOCKET email", $email)) {
  6. echo "Sent successfully!
    ";
  7. }else{
  8. echo "Failed to send!
    ";
  9. }
  10. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template