ホームページ  >  記事  >  バックエンド開発  >  PHP は SSL 接続をサポートする SMTP 電子メール送信クラスを実装します_PHP チュートリアル

PHP は SSL 接続をサポートする SMTP 電子メール送信クラスを実装します_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:05:051356ブラウズ

PHP は SSL 接続をサポートする SMTP メール送信クラスを実装します

この記事では、主に SSL 接続をサポートする SMTP メール送信クラスを実装する PHP を紹介し、SMTP メール送信クラスを実装し、SSL 接続をサポートする PHP の原理とテクニックを分析します。方法、困っている友達が参考にしてください

この記事の例では、SSL 接続をサポートする PHP での SMTP 電子メール送信クラスの実装について説明します。参考のためにみんなで共有してください。詳細は以下の通りです

このサンプル コードでは、gmail および QQ メールボックスの SMTP をテストしました。具体的なコードは次のとおりです:

コードは次のとおりです:

/**
*メール送信クラス
* プレーンテキストメールと HTML 形式のメール、複数の受信者、複数の CC、複数の秘密 CC、添付ファイル (単一または複数の添付ファイル) 付きの送信をサポートし、サーバーへの SSL 接続をサポートします
* 必須の php 拡張子:socket、Fileinfo、openssl。
※エンコード形式はUTF-8、送信エンコード形式はbase64です
* @example
* $mail = 新しい MySendMail();
* $mail->setServer("smtp@126.com", "XXXXX@126.com", "XXXXX"); //SMTPサーバーの設定、通常の接続方法
* $mail->setServer("smtp.gmail.com", "XXXXX@gmail.com", "XXXXX", 465, true); //SMTP サーバーとサーバーへの SSL 接続を設定します
* $mail->setFrom("XXXXX") //送信者を設定します
* $mail->setReceiver("XXXXX"); //受信者を設定、複数の受信者、複数回通話
* $mail->setCc("XXXX"); // CC、複数の CC、複数回の呼び出しを設定します
* $mail->setBcc("XXXXX"); // シークレット カーボン コピーを設定し、複数のシークレット カーボン コピーを複数回呼び出します
* $mail->addAttachment("XXXX"); //添付ファイルを追加、複数回呼び出します
* $mail->setMail("test", "test"); //メールの件名と内容を設定します
* $mail->sendMail() // 送信
*/
クラス MySendMail {
/**
* @var string メール転送エージェントのユーザー名
* @access protected
*/
保護された $_userName;
/**
* @var string メール転送エージェントのパスワード
* @access protected
*/
保護された $_password;
/**
* @var string メール転送プロキシサーバーのアドレス
* @access protected
*/
保護された $_sendServer;
/**
* @var int メール転送プロキシサーバーのポート
* @access protected
*/
保護された $_port;
/**
* @var 文字列送信者
* @access protected
*/
$_from から保護されました;
/**
* @var 配列受信者
* @access protected
*/
protected $_to = array();
/**
* @var 配列 CC
* @access protected
*/
保護された $_cc = array();
/**
* @var 配列 シークレット CC
* @access protected
*/
protected $_bcc = array();
/**
* @var 文字列テーマ
* @access protected
*/
保護された $_subject;
/**
* @var string メール本文
* @access protected
*/
保護された $_body;
/**
* @var 配列の添付ファイル
* @access protected
*/
protected $_attachment = array();
/**
* @var リソースソケットリソース
* @access protected
*/
保護された $_socket;
/**
* @var リソース 安全な接続ですか
* @access protected
*/
保護された $_isSecurity;
/**
* @var 文字列エラーメッセージ
* @access protected
*/
保護された $_errorMessage;
/**
※匿名でメールを送信できるサーバーであれば、プロキシサーバーのアドレスを渡すだけでメール転送プロキシを設定できます
* @アクセス公開
* @param string $server プロキシ サーバーの IP またはドメイン名
* @param string $username 認証アカウント
* @param string $password 認証パスワード
* @param int $port プロキシサーバーのポート、SMTP デフォルトポート 25
* @param boolean $isSecurity サーバーへの接続が安全な接続かどうか、デフォルトは false です
* @return boolean
*/
public function setServer($server, $username="", $password="", $port=25, $isSecurity=false) {
$this->_sendServer = $server;
$this->_port = $port;
$this->_isSecurity = $isSecurity;
$this->_userName = empty($username) ? "" :base64_encode($username);
$this->_password = empty($password) ? "" :base64_encode($password);
true を返します;
}
/**
* 送信者を設定します
* @アクセス公開
* @param string $from 送信者アドレス
* @return boolean
*/
パブリック関数 setFrom($from) {
$this->_from = $from;
true を返します;
}
/**
* 受信者を設定、複数の受信者、複数回通話可能
* @アクセス公開
* @param string $受信者のアドレスへ
* @return boolean
*/
パブリック関数 setReceiver($to) {
$this->_to[] = $to;
true を返します;
}
/**
* CC、複数の CC、複数回の通話を設定します。
* @アクセス公開
* @param string $cc CC アドレス
* @return boolean
*/
パブリック関数 setCc($cc) {
$this->_cc[] = $cc;
true を返します;
}
/**
* 秘密のカーボンコピー、複数の秘密のカーボンコピーを設定し、複数回呼び出します
* @アクセス公開
* @param string $bcc 秘密のカーボンコピーアドレス
* @return boolean
*/
パブリック関数 setBcc($bcc) {
$this->_bcc[] = $bcc;
true を返します;
}
/**
* メールの添付ファイル、複数の添付ファイル、複数回の通話を設定します
* @アクセス公開
* @param string $file ファイルアドレス
* @return boolean
*/
パブリック関数 addAttachment($file) {
if(!file_exists($file)) {
$this->_errorMessage = "ファイル " . $file . " が存在しません。";
false を返します;
}
$this->_attachment[] = $file;
true を返します;
}
/**
* メールメッセージを設定します
* @アクセス公開
* @param string $body メールの件名
* @param string $subject 電子メールのメインコンテンツ。プレーンテキストまたは HTML テキストにすることができます
* @return boolean
*/
パブリック関数 setMail($subject, $body) {
$this->_subject =base64_encode($subject);
$this->_body =base64_encode($body);
true を返します;
}
/**
*メールを送信
* @アクセス公開
* @return boolean
*/
パブリック関数 sendMail() {
$command = $this->getCommand();
$this->_isSecurity ? $this->socketSecurity() : $this->socket();
foreach ($command を $value) {
$result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]);
if($result) {
続けます;
}
他{
false を返します;
}
}
//実際には、ここで閉じる必要はありません。smtp コマンド QUIT が発行された後、サーバーは接続を閉じ、ローカル ソケット リソースは自動的に解放されます
$this->_isSecurity ? $this->closeSecuity() : $this->close();
true を返します;
}
/**
* エラーメッセージを返す
* @戻り文字列
*/
パブリック関数エラー(){
if(!isset($this->_errorMessage)) {
$this->_errorMessage = "";
}
$this->_errorMessage;
を返す }
/**
* メールコマンドに戻る
* @access protected
* @return 配列
*/
保護された関数 getCommand() {
$separator = "----=_Part_" . md5($this->_from . time()) //セパレーター
$command = 配列(
array("HELO sendmailrn", 250)
);
if(!empty($this->_userName)){
$command[] = array("AUTH LOGINrn", 334);
$command[] = array($this->_userName . "rn", 334);
$command[] = array($this->_password . "rn", 235);
}
//送信者を設定します
$command[] = array("MAIL FROM: <" . $this->_from . ">rn", 250);
$header = "FROM: <" . ">rn";
//受信者を設定します
if(!empty($this->_to)) {
$count = count($this->_to);
if($count == 1){
$command[] = array("RCPT TO: <" . $this->_to[0] . ">rn", 250);
$header .= "TO: <$this->_to[0] .">rn";
}
他{
for($i=0; $i $command[] = array("RCPT TO: <" . $this->_to[$i] . ">rn", 250);
if($i == 0){
$header .= "TO: < $this->_to[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<$this->_to[$i] .">rn";
}
他{
$header .= ",<$this->_to[$i] .">";
}
}
}
}
// CC を設定します
if(!empty($this->_cc)) {
$count = count($this->_cc);
if($count == 1){
$command[] = array("RCPT TO: <" . $this->_cc[0] . ">rn", 250);
$header .= "CC: _cc[0] .">rn";
}
他{
for($i=0; $i $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">rn", 250);
if($i == 0){
$header .= "CC: &​​lt; $this->_cc[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<$this->_cc[$i] .">rn";
}
他{
$header .= ",<$this->_cc[$i] .">";
}
}
}
}
//秘密のカーボンコピーを設定します
if(!empty($this->_bcc)) {
$count = count($this->_bcc);
if($count == 1) {
$command[] = array("RCPT TO: <" . $this->_bcc[0] . ">rn", 250);
$header .= "BCC: &​​lt; $this->_bcc[0] .">rn";
}
他{
for($i=0; $i$command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">rn", 250);
if($i == 0){
$header .= "BCC: &​​lt; $this->_bcc[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<$this->_bcc[$i] .">rn";
}
他{
$header .= ",<$this->_bcc[$i] .">";
}
}
}
}
//テーマ
$header .= "件名: =?UTF-8?B? $this->_subject ."?=rn";
if(isset($this->_attachment)) {
//添付ファイルを含むメールヘッダーは次のように宣言する必要があります
$header .= "Content-Type: multipart/mixed;rn";
}
elseif(false){
//メール本文には画像リソースが含まれており、メール内に含まれる画像が参照されているリモート画像の場合はこのように宣言されています
。 $header .= "Content-Type: multipart/relative;rn";
}
他{
//HTML またはプレーンテキストのメールは次のように宣言されます
$header .= "Content-Type: multipart/alternative;rn";
}
//メールヘッダーの区切り文字
$header .= "t" . 'boundary="' . $separator . '"';
$header .= "rnMIME バージョン: 1.0rn";
//ここからメールの本文部分が始まります。本文部分はいくつかの段落に分かれて送信されます
$header .= "rn--" .$separator .
$header .= "Content-Type:text/html; charset=utf-8rn";
$header .= "コンテンツ転送エンコーディング:base64rnrn";
$header .= $this->_body . "rn";
$header .= "--" . $separator ;
//添付ファイルを追加します
if(!empty($this->_attachment)){
$count = count($this->_attachment);
for($i=0; $i $header .= "rn--" .$separator .
$header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) '; name="=?UTF-8?B?' this->_attachment[$i]) ) . '?=' . "rn";
$header .= "コンテンツ転送エンコーディング:base64rn";
$header .= 'Content-Disposition:attachment; filename="=?UTF-8?B?' .base64_encode(basename($this->_attachment[$i]) ) . '?=' . ;
$header .= "rn";
$header .= $this->readFile($this->_attachment[$i]);
$header .= "rn--" .$separator .
}
}
//メールデータ送信終了
$header .= "rn.rn";

$command[] = array("DATArn", 354);
$command[] = 配列($header, 250);
$command[] = array("QUITrn", 221);
$コマンドを返す;
}
/**
* コマンドを送信
* @access protected
* @param string $command サーバーに送信された SMTP コマンド
* @param int $code サーバーから返される応答を期待していますか
* @return boolean
*/
保護された関数 sendCommand($command, $code) {
echo 'コマンドを送信します:' . $command . '
';
//サーバーにコマンドを送信
試してみてください{
if(socket_write($this->_socket, $command, strlen($command))){
//電子メールの内容が複数回送信されると、$code がなく、サーバーは戻りません
if(空($code)) {
true を返します;
}
//サーバーの戻り値を読み取ります
$data = トリム(socket_read($this->_socket, 1024));
echo 'response:' . $data '

';
if($data) {
$pattern = "/^".$code."+?/";
if(preg_match($pattern, $data)) {
true を返します;
}
他{
$this->_errorMessage = "エラー:" . $data . "|**| コマンド:";
false を返します;
}
}
他{
$this->_errorMessage = "エラー:" .socket_strerror(socket_last_error());
false を返します;
}
}
他{
$this->_errorMessage = "エラー:" .socket_strerror(socket_last_error());
false を返します;
}
}catch(例外 $e) {
$this->_errorMessage = "エラー:" . $e->getMessage();
}
}
/**
* 安全な接続を介してコマンドを送信します
* @access protected
* @param string $command サーバーに送信された SMTP コマンド
* @param int $code サーバーから返される応答を期待していますか
* @return boolean
*/
保護された関数 sendCommandSecurity($command, $code) {
echo 'コマンドを送信します:' . $command . '
';
試してみてください{
if(fwrite($this->_socket, $command)){
//メールの内容が複数回送信されると、$code がなく、サーバーは戻りません
if(空($code)) {
true を返します;
}
//サーバーの戻り値を読み取ります
$data = トリム(fread($this->_socket, 1024));
エコー '応答:' 。 $data 。 '

';
if($data) {
$pattern = "/^".$code."+?/";
if(preg_match($pattern, $data)) {
true を返します;
}
他{
$this->_errorMessage = "エラー:" 。 $data 。 "|**| コマンド:";
false を返します;
}
}
他{
false を返します;
}
}
他{
$this->_errorMessage = "エラー: " . $コマンド 。 「送信に失敗しました」;
false を返します;
}
}catch(例外 $e) {
$this->_errorMessage = "エラー:" 。 $e->getMessage();
}
}
/**
* 添付ファイルの内容を読み取り、base64 でエンコードされたファイルの内容を返します
* @access protected
* @param string $file ファイル
* @return 混合
*/
保護された関数 readFile($file) {
if(file_exists($file)) {
$file_obj = file_get_contents($file);
returnbase64_encode($file_obj);
}
他に{
$this->_errorMessage = "ファイル " . $file 。 「存在しません」;
false を返します;
}
}
/**
* 添付ファイルの MIME タイプを取得します
* @access protected
* @param string $file ファイル
* @return 混合
*/
保護された関数 getMIMEType($file) {
if(file_exists($file)) {
$mime = mime_content_type($file);
/*if(! preg_match("/gif|jpg|png|jpeg/", $mime)){
$mime = "アプリケーション/オクテットストリーム";
}*/
$mime を返します;
}
他に{
false を返します;
}
}
/**
* サーバーへのネットワーク接続を確立します
* @access protected
* @return boolean
*/
保護された関数ソケット() {
//创建ソケット资源
$this->_socket =ソケット_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if(!$this->_socket) {
$this->_errorMessage =ソケット_strerror(socket_last_error());
false を返します;
}
socket_set_block($this->_socket);//設置ブロックモード
//接続服务器
if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
$this->_errorMessage =ソケット_strerror(socket_last_error());
false を返します;
}
$str =socket_read($this->_socket, 1024);
if(!preg_match("/220+?/", $str)){
$this->_errorMessage = $str;
false を返します;
}
true を返します;
}
/**
* サーバーへのSSLネットワーク接続を確立します
* @access protected
* @return boolean
*/
保護された関数socketSecurity() {
$remoteAddr = "tcp://" 。 $this->_sendServer 。 「:」。 $this->_port;
$this->_socket = stream_socket_client($remoteAddr, $errno, $errstr, 30);
if(!$this->_socket){
$this->_errorMessage = $errstr;
false を返します;
}
// 密接な接続を設定します。SSL を使用します。TLS 接続が必要な場合は、php ハンドストリーム_socket_enable_crypto 関数の解を確認できます。 stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
stream_set_blocking($this->_socket, 1); //設置妨害モード
$str = fread($this->_socket, 1024);
if(!preg_match("/220+?/", $str)){
$this->_errorMessage = $str;
false を返します;
}
true を返します;
}
/**
* ソケットを閉じる
* @access protected
* @return boolean
*/
保護された関数 close() {
if(isset($this->_socket) && is_object($this->_socket)) {
$this->_socket->close();
true を返します;
}
$this->_errorMessage = "リソースを近づけることはできません";
false を返します;
}
/**
* セキュアソケットを閉じます
* @access protected
* @return boolean
*/
保護された関数 closeSecuity() {
if(isset($this->_socket) && is_object($this->_socket)) {
stream_socket_shutdown($this->_socket, STREAM_SHUT_WR);
true を返します;
}
$this->_errorMessage = "リソースを近づけることはできません";
false を返します;
}
}

ここで説明されている大家向けの php プログラムの設計が役立つことを望みます。

www.bkjia.com本当http://www.bkjia.com/PHPjc/963978.html技術記事 PHP は SSL 接続をサポートする SMTP メール送信クラスを実装します。この記事では、主に SSL 接続をサポートする SMTP メール送信クラスを実装する PHP を紹介し、サンプルを使用して SMTP メール送信クラスを実装する PHP を分析します。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。