PHPでメールを送信する方法2.(SMTP転送SMTPクラス)_PHPチュートリアル

WBOY
リリース: 2016-07-13 17:52:00
オリジナル
974 人が閲覧しました

[html]
定義('SMTP_STATUS_NOT_CONNECTED', 1, TRUE); 
定義('SMTP_STATUS_CONNECTED', 2, TRUE); 
 
クラス p8_smtp
{
    var $connection; 
    var $受信者; 
    var $headers; 
    var $timeout; 
    var $errors; 
    var $status; 
    var $body; 
    var $from; 
    var $host; 
    var $ポート; 
    var $helo; 
    var $auth; 
    var $user; 
    var $pass; 
     
    /**
* パラメータは配列です
* host SMTP サーバーのホスト デフォルト: localhost
* ポート SMTP サーバーのポート デフォルト: 25
* helo 送信された HELO コマンドの名前 デフォルト: localhost
* user SMTP サーバーのユーザー名 デフォルト: NULL
* * SMTP サーバーのログインパスワードを渡します デフォルト: null
* timeout 接続タイムアウト時間 デフォルト: 5
* * @return bool
​​*/
     
    関数 p8_smtp($params = array())
    {
        if(!define('CRLF'))define('CRLF', "rn", TRUE); 
         
        $this->タイムアウト = 5; 
        $this->ステータス = SMTP_STATUS_NOT_CONNECTED; 
        $this->host = 'ローカルホスト'; 
        $this->ポート = 25; 
        $this->auth = FALSE; 
        $this->ユーザー = ''; 
        $this->pass = ''; 
        $this->errors = array(); 
        foreach($params as $key => $value)
        {
            $this->$key = $value; 
        }
         
        $this->helo = $this->ホスト; 
        // 如果有設置用户名则不验证
        $this->auth = ('' == $this->user) ?真偽; 
    }
    関数 connect($params = array())
    {
        if(!isset($this->ステータス))
        {
            $obj = 新しい p8_smtp($params); 
             
            if($obj->connect())
            {
                $obj->ステータス = SMTP_STATUS_CONNECTED; 
            }
            $obj を返します。 
        }
        その他
        {
             
            $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); 
            ソケットセットタイムアウト($this->接続, 0, 250000); 
            $greeting = $this->get_data(); 
             
            if(is_resource($this->connection))
            {
                $this->ステータス = 2; 
                $this->auth を返しますか? $this->ehlo() : $this->helo(); 
            }
            その他
            {
                $this->errors[] = 'サーバーへの接続に失敗しました: '.$errstr; 
                FALSEを返します。 
            }
        }
    }
     
    /**
* パラメータは配列です
* 受信者 受信者の配列
* from * 送信者のアドレスは返信アドレスとしても使用されます
* headers * ヘッダー情報の配列
* 本文 * メールの本文
​​*/
     
    関数 send($params = array())
    {
        foreach($params as $key => $value)
        {
            $this->set($key, $value); 
        }
        if($this->is_connected())
        {
            // 服务器が必要かどうか验证
            if($this->auth)
            {
                if(!$this->auth()) は FALSE を返します。 
            }
            $this->mail($this->from); 
            if(is_array($this->recipients))
            {
                foreach($this->受信者を $value として)
                {
                    $this->rcpt($value); 
                }
            }
            その他
            {
                $this->rcpt($this->受信者); 
            }
            if(!$this->data()) は FALSE を返します。 
            $headers = str_replace(CRLF.'.', CRLF.'..', trim(implode(CRLF, $this->headers))); 
            $body = str_replace(CRLF.'.', CRLF.'..', $this->body); 
            $body = $body[0] == '.' ? '.'.$body : $body; 
            $this->send_data($headers); 
            $this->send_data(''); 
            $this->send_data($body); 
            $this->send_data('.'); 
            return (substr(trim($this->get_data()), 0, 3) === '250'); 
        }
        その他
        {
            $this->errors[] = '接続されていません!'; 
            FALSEを返します。 
        }
    }
     
    関数 helo()
    {
        if(is_resource($this->connection)
                および $this->send_data('HELO '.$this->helo)
                AND substr(trim($error = $this->get_data()), 0, 3) === '250' )
        {
            TRUEを返します。 
        }
        その他
        {
            $this->errors[] = 'HELO コマンドが失敗しました。出力: ' 。トリム(substr(トリム($error),3)); 
            FALSEを返します。 
        }
    }
     
     
    関数 ehlo()
    {
        if(is_resource($this->connection)
                および $this->send_data('EHLO '.$this->helo)
                AND substr(trim($error = $this->get_data()), 0, 3) === '250' )
        {
            TRUEを返します。 
        }
        その他
        {
            $this->errors[] = 'EHLO コマンドが失敗しました。出力: ' 。トリム(substr(トリム($error),3)); 
            FALSEを返します。 
        }
    }
     
    関数認証()
    {
        if(is_resource($this->connection)
                そして $this->send_data('AUTH LOGIN')
                AND substr(trim($error = $this->get_data()), 0, 3) === '334'
                AND $this->send_data(base64_encode($this->user)) // ユーザー名を送信します
                AND substr(trim($error = $this->get_data()),0,3) === '334'
                AND $this->send_data(base64_encode($this->pass)) // パスワードを送信します
                AND substr(trim($error = $this->get_data()),0,3) === '235' )
        {
            TRUEを返します。 
        }
        その他
        {
            $this->errors[] = 'AUTH コマンドが失敗しました: ' 。トリム(substr(トリム($error),3)); 
            FALSEを返します。 
        }
    }
     
    関数メール($from)
    {
        if($this->is_connected()
            および $this->send_data('MAIL FROM:<'.$from.'>')
            AND substr(trim($this->get_data()), 0, 2) === '250' )
        {
            TRUEを返します。 
        }
        その他
        {
            FALSEを返します。 
        }
    }
    関数 rcpt($to)
    {
        if($this->is_connected()
            および $this->send_data('RCPT TO:<'.$to.'>')
            AND substr(trim($error = $this->get_data()), 0, 2) === '25' )
        {
            TRUEを返します。 
        }
        その他
        {
            $this->errors[] = トリム(substr(trim($error), 3)); 
            FALSEを返します。 
        }
    }
    関数データ()
    {
        if($this->is_connected()
            そして $this->send_data('DATA')
            AND substr(trim($error = $this->get_data()), 0, 3) === '354' )
        {
            TRUEを返します。 
        }
        その他
        {
            $this->errors[] = トリム(substr(trim($error), 3)); 
            FALSEを返します。 
        }
    }
    関数 is_connected()
    {
        return (is_resource($this->connection) AND ($this->status === SMTP_STATUS_CONNECTED)); 
    }
    関数 send_data($data)
    {
        if(is_resource($this->connection))
        {
            return fwrite($this->connection, $data.CRLF, strlen($data)+2); 
        }
        その他
        {
            FALSEを返します。 
        }
    }
    関数 &get_data()
    {
        $return = ''; 
        $line = ''; 
        if(is_resource($this->connection))
        {
            while(strpos($return, CRLF) === FALSE OR substr($line,3,1) !== ' ')
            {
                $line = fgets($this->connection, 512); 
                $return .= $line; 
            }
            $return を返す; 
        }
        その他
        {
            FALSEを返します。 
        }
    }
    関数セット($var, $value)
    {
        $this->$var = $value; 
        TRUEを返します。 
    }
} // 授業終了
 
 
 
クラス SMTP
{
    var $debug; 
    var $host; 
    var $ポート; 
    var $auth; 
    var $user; 
    var $pass; 
 
    function smtp($host = "", $port = 25,$auth = false,$user,$pass){
        $this->host=$host; 
        $this->port=$port; 
        $this->auth=$auth; 
        $this->user=$user; 
        $this->pass=$pass; 
    }
 
    関数 sendmail($to,$from, $subject, $content, $T=0){
        グローバル $webdb; 
        //$name、$email、$subject、$content、$type=0
        $タイプ=1; 
        $name=array("{$webdb[webname]}会员"); 
        $email=array($to); 
        $_CFG['smtp_host']= $this->host; 
        $_CFG['smtp_port']= $this->port; 
        $_CFG['smtp_user']= $this->user; 
        $_CFG['smtp_pass']= $this->pass; 
        $_CFG['名前']= $webdb[ウェブ名]; 
        $_CFG['smtp_mail']= $from; 
 
        //$name = "=?UTF-8?B?".base64_encode($name)."==?=; 
        $subject = "=?gbk?B?".base64_encode($subject)."==?=; 
        $content = Base64_encode($content); 
        $headers[] = "To:=?gbk?B?".base64_encode($name[0])."?= <$email[0]>"; 
        $headers[] = "From:=?gbk?B?".base64_encode($_CFG[名前])."?= <$_CFG[smtp_mail]>"; 
        $headers[] = "MIME バージョン: Blueidea v1.0"; 
        $headers[] = "X-メーラー: 9gongyu メーラー v1.0"; 
        //$headers[] = "From:=?UTF-8?B?".base64_encode($_CFG['shop_name'])."==?=<$_CFG[smtp_mail]>"; 
        $headers[] = "件名:$件名"; 
        $headers[] = ($type == 0) ? "Content-Type: text/plain; charset=gbk; format=flowed" : "Content-Type: text/html; charset=gbk; format=flowed"; 
        $headers[] = "コンテンツ転送エンコーディング:base64"; 
        $headers[] = "コンテンツの配置: インライン"; 
        // SMTP サーバー情報
        $params['host'] = $_CFG['smtp_host']; 
        $params['ポート'] = $_CFG['smtp_port']; 
        $params['user'] = $_CFG['smtp_user']; 
        $params['pass'] = $_CFG['smtp_pass']; 
        if (empty($params['host']) || empty($params['port']))
        {
            // 設置主机と端末がある場合は false を直接返します
            false を返します。 
        }
        その他
        {
            // 送信邮件
            $send_params['受信者'] = $email; 
            $send_params['headers'] = $headers; 
            $send_params['from'] = $_CFG['smtp_mail']; 
            $send_params['body'] = $content; 
            /*
            echo "

"; <br>
            print_r($params); <br>
            print_r($send_params); <br>
            echo "
"; 
            出口; 
            */
            $smtp = 新しい p8_smtp($params); 
            if($smtp->connect() AND $smtp->send($send_params))
            {
                TRUEを返します。 
            }
            その他
            {
                FALSEを返します。 
            } //
の場合は終了         }
    }
}
 
?> 
作者:ベリロングモア

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/478151.html技術記事 [html] ?php 定義(SMTP_STATUS_NOT_CONNECTED, 1, TRUE);定義(SMTP_STATUS_CONNECTED, 2, TRUE);クラス p8_smtp { var $connection; var $受信者; var $headers; var $timeout; var $er...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!