php で smtp 経由で電子メールを送信するためのクラス、テスト pass_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:57:44
オリジナル
976 人が閲覧しました

smtp.php

コードをコピーします コードは次のとおりです:

class smtp
{
/* パブリック 変数 */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* プライベート 変数 */
var $sock;

/* コンストラクター */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //fsockopen() で使用されます
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; // HELO コマンドで使用されます
$this->log_file = "";
$this->靴下 = FALSE;
}

/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $Additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(.)","1.3", $body);
$header .= "MIME バージョン:1.0rn";
if($mailtype=="HTML")
{
$header .= "Content-Type:text/htmlrn";
}
$header .= "To: ".$to."rn";
if ($cc != "")
{
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $fromrn";
$header .= "件名: ".$subject."rn";
$header .= $Additional_headers;
$header .= "日付: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$TO = explode(",", $this->strip_comment($to));

if ($cc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to)
{
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to))
{
$this->log_write("エラー: 宛先にメールを送信できません。$rcpt_to."n");
$sent = FALSE;
続けます。
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
{ $this->log_write("電子メールが送信されました<".$rcpt_to.">n");
}} else
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("リモートホストnから切断されました");
}
$sent を返す;
}

/* プライベート 関数 */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd( 「こんにちは」、$ hello))
{
return $this->smtp_error("sending HELO command");
}

#auth
if($this->auth)
{
if (!$this->smtp_putcmd("AUTH LOGIN", base64_ encode($this->user)))
{
return $ this->smtp_error("HELO コマンドを送信中");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass)))

return $this-& gt;smtp_error("HELO コマンドを送信中");
}
}if(!$ this-&gt; smtp_putcmd( "mail"、 "from:&lt;"。$ from。 "&gt;")
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
{
return $this->smtp_error("送信中RCPT TO コマンド」 );
}
if (!$this->smtp_putcmd("DATA"))
{
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body))
{
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom())
{
return $this->smtp_error("送信中 . [EOM]) ;
}
if (!$this->smtp_putcmd("QUIT"))
{
return $this->smtp_error("sending QUIT command");
}
TRUEを返します。
}

function smtp_sockopen($address)
{
if ($this->relay_host == "")
{
return $this-> smtp_sockopen_mx($アドレス);
}
else
{
return $this->smtp_sockopen_relay();
}
}

function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_ポート。"n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if(!($ this-&gt; sock && $ this-&gt; smtp_ok​​())) n");
$this->log_write("エラー: ".$errstr." (".$errno.")n");
FALSE を返します。
}
$this->log_write("リレーホストに接続されました".$this->relay_host."n");
TRUE を返す;;
}

関数 smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "1", $address);
if (!@getmxrr($domain, $MXHOSTS))
{
$this->log_write("エラー: MX を解決できません"".$domain.""n");
FALSE を返します。
}
foreach ($MXHOSTS as $host)
{
$this->log_write("Trying to ".$host.":".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok​​()))
{
$this->log_write("警告: mx ホストに接続できません".$host ."n");
$this->log_write("エラー: ".$errstr." (".$errno.")n");
続けます。
}
$this->log_write("MX ホストに接続されました".$host."n");
TRUE を返します。
}
$this->log_write("エラー: どの mx ホストにも接続できません (".implode(", ", $MXHOSTS).")n");
FALSE を返します。
}

function smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn", "n".">gt; ", $header."n>gt; ".$body."n> "));
TRUE を返します。
}

関数 smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(". [EOM]n");
return $this->smtp_ok​​();
}

function smtp_ok​​()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response))
{
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("エラー: リモートホストが返されました"".$response.""n");
FALSE を返します。
}
TRUEを返します。
}

関数 smtp_putcmd($cmd, $arg = "")
{
if ($arg != "")
{
if($cmd=="")
{
$cmd = $arg;
}
else
{
$cmd = $cmd." ".$arg;
}
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug(">gt; ".$cmd."n");
return $this->smtp_ok​​();
}

関数 smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".n");
FALSE を返します。
}

関数log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "")
{
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
{
$this->smtp_debug("警告:ログ ファイルを開けません。"".$this->log_file.""n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
TRUE を返します。
}

関数 strip_comment($address)
{
$comment = "([^()]*)";
while (ereg($comment, $address))
{
$address = ereg_replace($comment, "", $address);
}
$address を返す;
}

関数 get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "1", $address);
$address を返す;
}

function smtp_debug($message)
{
if ($this->debug)
{
echo $message;
}
}

}
?>

テストプログラム
コードをコピーします コードは次のとおりです:

/*
これはテストプログラムです!!!
*/
require("smtp.php") );
# ####################################
$smtpserver = "smtp. 163. com";//SMTP サーバー
$smtpserverport = 25;//SMTP サーバー ポート
$smtpusermail = "reterry123@163.com";//SMTP サーバー ユーザーの電子メール
$smtpemailto = "dxytools@gmail.com"; / /送信先
$smtpuser = "reterry123";//SMTP サーバーのユーザーアカウント
$smtppass = "014040001";//SMTP サーバーのユーザーパスワード
$mailsubject = "中国語";//メールの件名
$ mailbody = "

中国語

新年感を出すためにテスト済み";//メール内容
$mailtype = "HTML";//メール形式(HTML/TXT)、TXTはテキストメールです
#### ###################################
$smtp = 新しい smtp($smtpserver,$ smtpserverport ,true,$smtpuser,$smtppass);//ここで true は認証が使用されることを意味し、そうでない場合は認証が使用されません
$smtp->debug = TRUE;//送信されたデバッグ情報を表示するかどうか
$smtp ->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);

http://www.bkjia.com/PHPjc/317764.html

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