Heim  >  Artikel  >  Backend-Entwicklung  >  linux下php配置smtp发送邮件的方法

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

WBOY
WBOYOriginal
2016-07-25 08:54:431612Durchsuche
  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=' }else
  27. $m='>> ';
  28. if(!ereg("\n$", $message))
  29. $message .= "
    ";
  30. $message=nl2br($message);
  31. echo "${m}${message}";
  32. }
  33. }
  34. //执行传递的命令
  35. function do_command($command, $code) {
  36. $this->lastact=$command;
  37. $this->show_debug($this->lastact, "out");
  38. fputs ( $this->fp, $this->lastact );
  39. $this->lastmessage = fgets ( $this->fp, 512 );
  40. $this->show_debug($this->lastmessage, "in");
  41. if(!ereg("^$code", $this->lastmessage))
  42. return false;
  43. else
  44. return true;
  45. }
  46. //邮件发送处理
  47. function send( $to,$from,$subject,$message) {
  48. //连接服务器
  49. $this->lastact="connect";
  50. $this->show_debug("连接到SMTP 服务器: ".$this->smtp, "out");
  51. $this->fp = fsockopen ( $this->smtp, $this->port );
  52. if ( $this->fp ) {
  53. $this->set_socket_blocking( $this->fp, true );
  54. $this->lastmessage=fgets($this->fp,512);
  55. $this->show_debug($this->lastmessage, "in");
  56. if (! ereg ( "^220", $this->lastmessage ) ) {
  57. return false;
  58. }else{
  59. $this->lastact="HELO " . $this->welcome . "\n";
  60. if(!$this->do_command($this->lastact, "250")){
  61. fclose($this->fp);
  62. return false;
  63. }
  64. $this->lastact="MAIL FROM: $from" . "\n";
  65. if(!$this->do_command($this->lastact, "250")){
  66. fclose($this->fp);
  67. return false;
  68. }
  69. $this->lastact="RCPT TO: $to" . "\n";
  70. if(!$this->do_command($this->lastact, "250")){
  71. fclose($this->fp);
  72. return false;
  73. }
  74. //开始发送邮件正文
  75. $this->lastact="DATA\n";
  76. if(!$this->do_command($this->lastact, "354")){
  77. fclose($this->fp);
  78. return false;
  79. }
  80. //开始处理邮件主题头
  81. $head="Subject: $subject\n";
  82. if(!empty($subject) && !ereg($head, $message)){
  83. $message = $head.$message;
  84. }
  85. //开始处理邮件From头
  86. $head="From: $from\n";
  87. if(!empty($from) && !ereg($head, $message)) {
  88. $message = $head.$message;
  89. }
  90. //开始处理邮件To头
  91. $head="To: $to\n";
  92. if(!empty($to) && !ereg($head, $message)) {
  93. $message = $head.$message;
  94. }
  95. //处理结束串
  96. if(!ereg("\n\.\n", $message))
  97. $message .= "\n.\n";
  98. $this->show_debug($message, "out");
  99. fputs($this->fp, $message);
  100. $this->lastact="QUIT\n";
  101. if(!$this->do_command($this->lastact, "250")){
  102. fclose($this->fp);
  103. return false;
  104. }
  105. }
  106. return true;
  107. }else{
  108. $this->show_debug("连接失败!!", "in");
  109. return false;
  110. }
  111. }
  112. }
  113. ?>
复制代码

使用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. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn