当前位置:Gxlcms > PHP教程 > linux下php配置smtp发送邮件的方法

linux下php配置smtp发送邮件的方法

时间:2021-07-01 10:21:17 帮助过:51人阅读

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

  2. /**
  3. * 定义邮件模块配制信息
  4. */
  5. define("SMTP_HOST","smtp.mail.yahoo.com"); // SMTP 主机
  6. define("SMTP_MAIL"," XXXX@yahoo.cn"); // SMTP 用户email
  7. define("SMTP_PASS"," XXXX"); // SMTP 用的密码

  8. define("SERVICE_MAIL"," XXXX@yahoo.cn"); // SMTP 用户email

  9. define("SERVICE_NAME","PHPBOOK邮件测试"); // SMTP 用的名字

  10. /**

  11. * 使用phpmailer发邮件模块
  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(); // 设置使用SMTP
  24. $mail->Host = SMTP_HOST; // 设置SMTP服务器地址
  25. $mail->SMTPAuth = true; // 打开SMTP权限验证
  26. $mail->Username = SMTP_MAIL; // SMTP 用户名
  27. $mail->Password = SMTP_PASS; // SMTP 服务器密码

  28. $mail->From = SERVICE_MAIL; // 设置发送者地址

  29. $mail->FromName = SERVICE_NAME; // 设置发送者名字
  30. $mail->AddAddress($email, $user); // 添加接收者地址
  31. $mail->AddReplyTo(SERVICE_MAIL, SERVICE_NAME); // 设置回复地址

  32. $mail->WordWrap = 50; // 设置显示格式

  33. $mail->IsHTML(true); // 设置邮件支持html
  34. $mail->Subject = $subject;
  35. $mail->Body = $body;
  36. $mail->AltBody = ""; // 文本类型的邮件

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

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

  43. //开始发送测试邮件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 = "邮件测试示例!"; // 发给用户的邮件标题小组
  47. $_mailBody = "新浪网"; // 邮件内容小组
  48. sendMail($tomail,$user,$_mailSubject,$_mailBody);
  49. ?>

实验证明yahoo的smtp很好用,号称sina的其实并不好用,我卡在着好长时间。

方法四,给予socket编写的程序 使用socket发送邮件的封装类:

  1. class sendmail{
  2. var $lastmessage; //记录最后返回的响应信息
  3. var $lastact; //最后的动作,字符串形式
  4. var $welcome; //用在HELO后面,欢迎用户
  5. var $debug; //是否显示调试信息
  6. var $smtp; //smtp服务器
  7. var $port; //smtp端口号
  8. var $fp; //socket句柄
  9. //发送邮件函数
  10. function send_mail($smtp, $welcome="", $debug=false) {
  11. if(empty($smtp)) die("SMTP不能为空!");
  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="DATA\n";
  77. if(!$this->do_command($this->lastact, "354")){
  78. fclose($this->fp);
  79. return false;
  80. }
  81. //开始处理邮件主题头
  82. $head="Subject: $subject\n";
  83. if(!empty($subject) && !ereg($head, $message)){
  84. $message = $head.$message;
  85. }
  86. //开始处理邮件From头
  87. $head="From: $from\n";
  88. if(!empty($from) && !ereg($head, $message)) {
  89. $message = $head.$message;
  90. }
  91. //开始处理邮件To头
  92. $head="To: $to\n";
  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="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("连接失败!!", "in");
  110. return false;
  111. }
  112. }
  113. }
  114. ?>

使用socket发送邮件示例:

  1. include ("./sendmail.class.php");
  2. $mail = new sendmail();
  3. $email = "您好,这是一个测试邮件!";
  4. $sendmail = new send_mail("smtp.mail.126.com","PHPBOOK",true); //显示调示信息
  5. if($mail->send("XXXX@126.com", "XXXX@126.com", "测试SOCKET邮件", $email)) {
  6. echo "发送成功!
    ";
  7. }else{
  8. echo "发送失败!
    ";
  9. }
  10. ?>

人气教程排行