⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.smtpmail.php

📁 用php编写的一个BBS 小程序
💻 PHP
字号:
<?php
/**
 * class.SmtpMail.php
 * 2007-08-08
 */
class SmtpMail {

    private $from;
    private $to;
    private $subject;
    private $message;
    private $check;
    private $smtp;
    private $fp;
    private $username;
    private $password;
    private $error;
    private $showError;

    public $errno;
    public $errstr;

    public function __construct($chk = 0, $usr = "", $pwd = "", $svr = "") {
        $this->smtp = defined('SMTP') ? SMTP : ($svr != "" ? $svr : "localhost");
        $this->check = defined('CHECK') && CHECK == 1 ? 1 : ($chk != 0 ? 1 : 0);
        $this->user = $usr != "" ? $usr : "anonymous";
        $this->password = $pwd != "" ? $pwd : "";
        $this->error = "";
        $this->showError = false;
    }

    public function __destruct() {
        //TODO
    }

    public function send_mail($from, $to, $subject, $message) {
        $this->from = $from;
        $this->to = $to;
        $this->subject = $subject;
        $this->message = $message;
        $this->error = "";
        $this->fp = fsockopen($this->smtp, 25, $this->errno, $this->errstr, 60);
        if (!$this->fp) {
            return $this->showError ? "ERROR: " . $this->errno . " - " . $this->errstr : "";
        }
        stream_set_blocking($this->fp, true);
        $this->error = fgets($this->fp, 512);
        if (substr($this->error, 0, 3) != 220) {
            return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
        }
        if ($this->check) {
            fwrite($this->fp, "EHLO " . $this->username . "\r\n");
        } else {
            fwrite($this->fp, "HELO " . $this->username . "\r\n");
        }
        $this->error = fgets($this->fp, 512);
        if (substr($this->error, 0, 3) != 250) {
            return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
        }
        if ($this->check) {
            while (true) {
                $this->error = fgets($this->fp, 512);
                if ( substr($this->error, 3, 1) != "-" || empty($this->error) ) {
                    break;
                }
            }
            $this->username == "" ? $this->username = "anonymous" : NULL;
            fwrite($this->fp, "AUTH LOGIN\r\n");
            $this->error = fgets($this->fp, 512);
            if (substr($this->error, 0, 3) != 334) {
                return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
            }
            fwrite($this->fp, base64_encode($this->username) . "\r\n");
            $this->error = fgets($this->fp, 512);
            if (substr($this->error, 0, 3) != 334) {
                return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
            }
            fwrite($this->fp, base64_encode($this->password) . "\r\n");
            $this->error = fgets($this->fp, 512);
            if (substr($this->error, 0, 3) != "235") {
                return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
            }
        }
        fwrite($this->fp, "MAIL FROM: " . $this->from . "\r\n");
        $this->error = fgets($this->fp, 512);
        if (substr($this->error, 0, 3) != 250) {
            return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
        }
        fwrite($this->fp, "RCPT TO: " . $this->to . "\r\n");
        $this->error = fgets($this->fp, 512);
        if (substr($this->error, 0, 3) != 250) {
            return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
        }
        fwrite($this->fp, "DATA\r\n");
        $this->error = fgets($this->fp, 512);
        if (substr($this->error, 0, 3) != 354) {
            return $this->showError ? "ERROR: " . $this->error . "(" . $this->to . ")<br />\n" : "";
        }
        $this->message = "To: " . $this->to . "\r\nFrom: " . $this->from . "\r\nSubject: " . $this->subject . "\r\n" . $this->message . "\r\n.\r\n";
        fwrite($this->fp, $this->message);
        fwrite($this->fp, "QUIT\r\n");
        fclose($this->fp);
        return 0;
    }

    public function set_username($username) {
        $this->username = $username;
    }

    public function set_password($password) {
        $this->password = $password;
    }

    public function set_showError($showError) {
        $this->showError = $showError;
    }

}
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -