找回密码
 注册
搜索
查看: 3850|回复: 0

[转载]SMTP协议-PHP的邮件发送程序例子

[复制链接]
发表于 2004-6-2 13:10:29 | 显示全部楼层 |阅读模式
<?php
  class ZSMailBox
  {
    var $fpSocket;
    var $strLog;

    var $strSMTPServer;
    var $strSMTPPort;
    var $strFrom;
    var $strTo;

    function ZSMailBox ()
    {
      $this->strLog = "";
      $this->strSMTPPort = "25";
      $this->strFrom = "";
      $this->strTo = "";
    }

    function DoCommand ($strCommand, $strOKReply)
    {
      fputs ($this->fpSocket, $strCommand);
      $strReply = fgets ($this->fpSocket, 512);
      if(! ereg("^$strOKReply", $strReply))
      {
        return false;
      }

      return true;
    }

    function WaitReply ($strOKReply)
    {
      $strReply = fgets ($this->fpSocket, 512);
      $this->strLog .= "ReplystrReply" . "<br>n";
      if (! ereg ("^$strOKReply", $strReply))
      {
        return false;
      }

      return true;
    }

    function SendData ($strSubject, $strContent)
    {
      $str = "To: " . $this->strTo . chr(13) . chr(10);
      $str .= "From: " . $this->strFrom . chr(13) . chr(10);
      $str .= "Subject: " . $strSubject . chr(13) . chr(10) . chr(13) . chr(10);
      $str .= $strContent;
      $str .= chr(13) . chr (10) . "." . chr(13) . chr(10);

      fputs ($this->fpSocket, $str);

      return true;
    }

    function SendMail ($strSubject, $strContent)
    {
      if ($this->VerifySMTPVariables ())
      {
        $this->strLog = "";
      } else
      {
        $this->strLog = "Any of SMTP variables are invaild<br>n";
        return false;
      }

      $this->fpSocket = fsockopen ($this->strSMTPServer, $this->strSMTPPort);
      if (! $this->fpSocket)
      {
        $this->strLog .= "Cann't open socket<br>n";
        return false;
      }
      $this->strLog .= "Open socket succeed<br>n";

      if (! $this->WaitReply ("220"))
      {
        fclose ($this->fpSocket);
        return false;
      }
      $strCommand = "HELO " . $this->strSMTPServer . "n";
      if (! $this->DoCommand ($strCommand, "250"))
      {
        $this->strLog .= "HELO error<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      $strCommand = "MAIL FROM: <" . $this->strTo . ">n";
      if (! $this->DoCommand ($strCommand, "250"))
      {
        $this->strLog .= "MAIL error<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      $strCommand = "RCPT TO: <" . $this->strTo . ">n";
      if (! $this->DoCommand ($strCommand, "250"))
      {
        $this->strLog .= "Refuse to establish the channel<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      $strCommand = "DATAn";
      if (! $this->DoCommand ($strCommand, "354"))
      {
        $this->strLog .= "Refuse to recieve the data<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      if (! $this->SendData ($strSubject, $strContent))
      {
        $this->strLog .= "Send data error<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      if (! $this->WaitReply ("250"))
      {
        $this->strLog .= "Send data unsuccessful<br>n";
        fclose ($this->fpSocket);
        return false;
      }
      $strCommand = "QUITn";
      if (! $this->DoCommand ($strCommand, "221"))
      {
        $this->strLog .= "QUIT error<br>n";
        fclose ($this->fpSocket);
        return false;
      }

      fclose ($this->fpSocket);

      return true;
    }

    function VerifyMailAddr ($strMailAddr)
    {
      return (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$",$strMailAddr));
    }

    function VerifyWebSiteAddr ($strWebSiteAddr)
    {
      return (eregi ("^([_0-9a-z-]+.)+[a-z]{2,3}$", $strWebSiteAddr));
    }

    function VerifySMTPVariables ()
    {
      if (! $this->VerifyWebSiteAddr ($this->strSMTPServer))
        return false;
      if (! isset ($this->strSMTPPort))
        return false;
      if (! $this->VerifyMailAddr ($this->strFrom))
        return false;
      if (! $this->VerifyMailAddr ($this->strTo))
        return false;

      return true;
    }

    function SetSMTPVariables ($strServer, $strPort, $strFrom, $strTo)
    {
      if (isset ($strServer))
        $this->strSMTPServer = $strServer;
      if (isset ($strPort))
        $this->strSMTPPort = $strPort;
      if (isset ($strFrom))
        $this->strFrom = $strFrom;
      if (isset ($strTo))
        $this->strTo = $strTo;
    }
  }
?>
<html>
<head>
<title>ZSMail</title>
<style type="text/css">
<!--
.ftNormal {font-size: 9pt}
.aToolBar {font-size:10pt; color:#006699; text-decoration:none;}
.aToolBar:hover {font-size:10pt; color:red; text-decoration:underline;}
.aNormal {font-size:9pt; color:#006699; text-decoration:none;}
.aNormal:hover{font-size:9pt; color:red; text-decoration:none;}
.aBig {font-size:12pt; color:#ffffec; text-decoration:none;}
.aBig:hover{font-size:12pt; color:#ffffec; text-decoration:none;}
-->
</style>
</head>
<body>
<?php
  if (isset ($strTo) && ($strTo != ""))
  {
    if (isset ($nServer))
    {
      switch ($nServer)
      {
        case 1: $strSMTPServer = "smtp.sina.com.cn";
                $strSMTPPort = "25";
                $strTo .= "@sina.com";
                break;
        case 2: $strSMTPServer = "smtp.163.net";
                $strSMTPPort = "25";
                $strTo .= "@163.net";
                break;
        case 3: $strSMTPServer = "smtp.yeah.net";
                $strSMTPPort = "25";
                $strTo .= "@yeah.net";
                break;
        case 4: $strSMTPServer = "smtp.netease.com";
                $strSMTPPort = "25";
                $strTo .= "@netease.com";
                break;
        case 5: $strSMTPServer = "smtp.sohu.com";
                $strSMTPPort = "25";
                $strTo .= "@sohu.com";
                break;
        case 6: $strSMTPServer = "smtp.263.net";
                $strSMTPPort = "25";
                $strTo .= "@263.net";
                break;
        default: $strSMTPServer = "smtp.sina.com.cn";
                 $strSMTPPort = "25";
                 $strTo = "guestxyz@sina.com";
                 break;
      }
    }
    if (! isset ($strFrom) || ($strFrom == ""))
    {
      $strFrom = $strTo;
    }
    if (! isset ($strSubject) || ($strSubject == ""))
    {
      $strSubject = "No subject";
    }
    if (! isset ($strContent) || ($strContent == ""))
    {
      $strContent = "No content";
    }

    $zsmb = new ZSMailBox;
    $zsmb->SetSMTPVariables ($strSMTPServer, $strSMTPPort, $strFrom, $strTo);
    if ($zsmb->SendMail ($strSubject, $strContent))
    {
      printf ("OK");
    } else
    {
      printf ($zsmb->strLog);
    }
  }
?>
<hr>
<form action="ZSMail.php3" method=POST>
<table>
  <tr><td>From</td>
      <td><input type=text name=strFrom></td>
  </tr>
  <tr><td>To</td>
      <td><input type=text name=strTo>@
          <select name=nServer>
            <option value=1 selected>sina.com</option>
            <option value=2>163.net</option>
            <option value=3>yeah.net</option>
            <option value=4>netease.com</option>
            <option value=5>sohu.com</option>
            <option value=6>263.net</option>
          </select>
      </td>
  </tr>
  <tr><td>Subject</td>
      <td><input type=text name=strSubject></td>
  </tr>
  <tr><td>Content</td>
      <td><textarea name=strContent rows=4 cols=64></textarea>
      </td>
  </tr>
  <tr><td><input type=submit value=发送></td>
      <td></td>
  </tr>
</table>
</form>
</body>
</html>  

经测试,只能用于没有验证用户的smtp服务器。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2025-5-5 06:52 , Processed in 0.014762 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表