php指定smtp服务器发邮件

回复 收藏
以前一直都使用linux服务器自带的sendmail服务,但是这个很不稳定,经常发送不出去,所以想到使用php程序指定smtp服务器来发送邮件。把下面的代码保存到一个php文件内,例如 mail.php
  1. <?php
  2. class Smtp
  3. {
  4.     /* Public Variables */
  5.     var $smtp_port;
  6.     var $time_out;
  7.     var $host_name;
  8.     var $log_file;
  9.     var $relay_host;
  10.     var $debug;
  11.     var $auth;
  12.     var $user;
  13.     var $pass;
  14.     /* Private Variables */
  15.     var $sock;
  16.     /* Constractor */
  17.     function Smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
  18.     {
  19.         $this->debug = FALSE;
  20.         $this->smtp_port = $smtp_port;
  21.         $this->relay_host = $relay_host;
  22.         $this->time_out = 30; //is used in fsockopen()
  23.         #
  24.         $this->auth = $auth;//auth
  25.         $this->user = $user;
  26.         $this->pass = $pass;
  27.         #
  28.         $this->host_name = "localhost"; //is used in HELO command
  29.         $this->log_file = "";
  30.         $this->sock = FALSE;
  31.     }
  32.     /* Main Function */
  33.     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
  34.     {
  35.         $mail_from = $this->get_address($this->strip_comment($from));
  36.         $body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
  37.         $header = "MIME-Version:1.0\r\n";
  38.         if($mailtype=="HTML"){
  39.             $header .= "Content-Type:text/html\r\n";
  40.         }
  41.         $header .= "To: ".$to."\r\n";
  42.         if ($cc != "") {
  43.             $header .= "Cc: ".$cc."\r\n";
  44.         }
  45.         $header .= "From: $from<".$from.">\r\n";
  46.         $header .= "Subject: ".$subject."\r\n";
  47.         $header .= $additional_headers;
  48.         $header .= "Date: ".date("r")."\r\n";
  49.         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
  50.         list($msec, $sec) = explode(" ", microtime());
  51.         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
  52.         $TO = explode(",", $this->strip_comment($to));
  53.         if ($cc != "") {
  54.             $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
  55.         }
  56.         if ($bcc != "") {
  57.             $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
  58.         }
  59.         $sent = TRUE;
  60.         foreach ($TO as $rcpt_to) {
  61.             $rcpt_to = $this->get_address($rcpt_to);
  62.             if (!$this->smtp_sockopen($rcpt_to)) {
  63.                 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
  64.                 $sent = FALSE;
  65.                 continue;
  66.             }
  67.             if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  68.                 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
  69.             } else {
  70.                 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
  71.                 $sent = FALSE;
  72.             }
  73.             fclose($this->sock);
  74.             $this->log_write("Disconnected from remote host\n");
  75.         }
  76.         return $sent;
  77.     }
  78. /* Private Functions */
  79.    function smtp_send($helo, $from, $to, $header, $body = "")
  80.     {
  81.         if (!$this->smtp_putcmd("HELO", $helo)) {
  82.             return $this->smtp_error("sending HELO command");
  83.         }
  84.         #auth
  85.         if($this->auth){
  86.             if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
  87.                 return $this->smtp_error("sending HELO command");
  88.             }
  89.             if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  90.                 return $this->smtp_error("sending HELO command");
  91.             }
  92.         }
  93.         #
  94.         if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
  95.             return $this->smtp_error("sending MAIL FROM command");
  96.         }
  97.         if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
  98.             return $this->smtp_error("sending RCPT TO command");
  99.         }
  100.         if (!$this->smtp_putcmd("DATA")) {
  101.             return $this->smtp_error("sending DATA command");
  102.         }
  103.         if (!$this->smtp_message($header, $body)) {
  104.             return $this->smtp_error("sending message");
  105.         }
  106.         if (!$this->smtp_eom()) {
  107.             return $this->smtp_error("sending . [EOM]");
  108.         }
  109.         if (!$this->smtp_putcmd("QUIT")) {
  110.             return $this->smtp_error("sending QUIT command");
  111.         }
  112.         return TRUE;
  113.     }
  114.     function smtp_sockopen($address)
  115.     {
  116.         if ($this->relay_host == "") {
  117.             return $this->smtp_sockopen_mx($address);
  118.         } else {
  119.             return $this->smtp_sockopen_relay();
  120.         }
  121.     }
  122.     function smtp_sockopen_relay()
  123.     {
  124.         $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
  125.         $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
  126.         if (!($this->sock && $this->smtp_ok())) {
  127.             $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
  128.             $this->log_write("Error: ".$errstr." (".$errno.")\n");
  129.             return FALSE;
  130.         }
  131.         $this->log_write("Connected to relay host ".$this->relay_host."\n");
  132.         return TRUE;
  133.     }
  134.     function smtp_sockopen_mx($address)
  135.     {
  136.         $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
  137.         if (!@getmxrr($domain, $MXHOSTS)) {
  138.             $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
  139.             return FALSE;
  140.         }
  141.         foreach ($MXHOSTS as $host) {
  142.             $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
  143.            $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
  144.             if (!($this->sock && $this->smtp_ok())) {
  145.                 $this->log_write("Warning: Cannot connect to mx host ".$host."\n");
  146.                 $this->log_write("Error: ".$errstr." (".$errno.")\n");
  147.                continue;
  148.             }
  149.             $this->log_write("Connected to mx host ".$host."\n");
  150.             return TRUE;
  151.         }
  152.         $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
  153.        return FALSE;
  154.     }
  155.     function smtp_message($header, $body)
  156.     {
  157.       fputs($this->sock, $header."\r\n".$body);
  158.         $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
  159.         return TRUE;
  160.     }
  161.     function smtp_eom()
  162.    {
  163.         fputs($this->sock, "\r\n.\r\n");
  164.         $this->smtp_debug(". [EOM]\n");
  165.         return $this->smtp_ok();
  166.     }
  167.     function smtp_ok()
  168.    {
  169.         $response = str_replace("\r\n", "", fgets($this->sock, 512));
  170.         $this->smtp_debug($response."\n");
  171.         if (!ereg("^[23]", $response)) {
  172.             fputs($this->sock, "QUIT\r\n");
  173.             fgets($this->sock, 512);
  174.             $this->log_write("Error: Remote host returned \"".$response."\"\n");
  175.             return FALSE;
  176.         }
  177.         return TRUE;
  178.     }
  179.     function smtp_putcmd($cmd, $arg = "")
  180.     {
  181.         if ($arg != "") {
  182.            if($cmd=="") $cmd = $arg;
  183.             else $cmd = $cmd." ".$arg;
  184.         }
  185.         fputs($this->sock, $cmd."\r\n");
  186.         $this->smtp_debug("> ".$cmd."\n");
  187.         return $this->smtp_ok();
  188.     }
  189.     function smtp_error($string)
  190.     {
  191.         $this->log_write("Error: Error occurred while ".$string.".\n");
  192.         return FALSE;
  193.     }
  194.     function log_write($message)
  195.     {
  196.         $this->smtp_debug($message);
  197.         if ($this->log_file == "") {
  198.                 return TRUE;
  199.         }
  200.         $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  201.         if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  202.             $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
  203.             return FALSE;;
  204.         }
  205.         flock($fp, LOCK_EX);
  206.         fputs($fp, $message);
  207.         fclose($fp);
  208.         return TRUE;
  209.     }
  210.     function strip_comment($address)
  211.    {
  212.         $comment = "\([^()]*\)";
  213.        while (ereg($comment, $address)) {
  214.             $address = ereg_replace($comment, "", $address);
  215.         }
  216.         return $address;
  217.     }
  218.     function get_address($address)
  219.     {
  220.         $address = ereg_replace("([ \t\r\n])+", "", $address);
  221.         $address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
  222.         return $address;
  223.     }
  224.     function smtp_debug($message)
  225.     {
  226.         if ($this->debug) {
  227.         echo $message;
  228.         }
  229.     }
  230. }
  231. $file = $argv[2];
  232. $smtpserver = "smtp.qq.com";//SMTP服务器
  233. $smtpserverport =25;//SMTP服务器端口
  234. $smtpusermail = "1198658@qq.com";//SMTP服务器的用户邮箱
  235. $smtpemailto = "lishi@139.com";//发送给谁
  236. $smtpuser = "1198658";//SMTP服务器的用户帐号
  237. $smtppass = "1212lss";//SMTP服务器的用户密码
  238. $mailsubject = $argv[1];//邮件主题
  239. $mailbody = file_get_contents($file);//邮件内容
  240. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  241. $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  242. //$smtp->debug = TRUE;//是否显示发送的调试信息
  243. $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
  244. ?>
要想发邮件的话,运行
php  mail.php  "邮箱主题写在这里" "/tmp/test.txt"
其中,/tmp/test.txt 内容为邮件内容。
2012-12-12 14:41 举报
已邀请:
0

江歌

赞同来自:

课后练习一下

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: