ホームページ >バックエンド開発 >PHPチュートリアル >電子メール送信 class_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->ホスト名 = "ローカルホスト"; //HELO コマンドで使用されます
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from ,$fromname , $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-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/html;charset=utf-8rn";
}
$header .= "宛先: ".$to."rn" ;
if ($cc != ""){
$header .= "Cc: ".$cc."rn";
}
$header .= "From: $fromname<".$fromname."> rn";
$header .= "件名: ".$subject."rn";
$header .= $Additional_headers;
$header .= "日付: ".date("r")."rn";
$header .= "X-Mailer:By Redhat (PHP/".php教程version().")rn";
list($msec, $sec) =explode(" ", microtime());
$header .= "メッセージ ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
$TO = 爆発(",", $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{
$ this->log_write("エラー: <".$rcpt_to.">n" にメールを送信できません);
$sent = FALSE;
}
fclose($this->sock);
$this- >log_write("リモートホストnから切断されました");
}
return $sent;
}
/* プライベート関数 */
function smtp_send($helo, $from, $to, $header, $body = ""){
if (!$this->smtp_putcmd("HELO", $helo)){
return $this->smtp_error("HELO コマンドの送信中");
}
#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->smtp_error("HELO コマンドの送信中");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from. ">")){
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to) .">gt;")){
return $this->smtp_error("RCPT TO コマンドを送信中");
}
if (!$this->smtp_putcmd("DATA")){
return $this->gt ;smtp_error("DATA コマンドの送信");
}
if (!$this->smtp_message($header, $body)){
return $this->smtp_error("メッセージの送信");
}
if (!$this->smtp_eom()){
return $this->smtp_error("
}
if (!$this->smtp_putcmd("QUIT")){
return $this->smtp_error("QUIT コマンドの送信中");
}
return TRUE;
}
function smtp_sockopen($address){
if ($this->relay_host == ""){
return $this->smtp_sockopen_mx($address);
}
else{
return $this->smtp_sockopen_relay( );
}
}
function smtp_sockopen_relay(){
$this->log_write("".$this->relay_host." を試行しています。":".$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())){
$this->log_write("エラー: リレー ホスト ".$this->relay_host."n" に接続できません。");
$this->log_write("エラー: ".$errstr." (".$errno.")n");
return FALSE;
}
$this->log_write("リレー ホスト ".$this->relay_host."n ");
return TRUE;;
}
function smtp_sockopen_mx($address){
$domain = ereg_replace("^.+@([^@]+)$", "1", $address);
if (!@getmxrr($domain, $MXHOSTS)){
$this->log_write("エラー: MX "".$domain.""n");
return 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" に接続しました);
return TRUE;
}
$this->log_write("エラー: どの mx にも接続できませんhosts (".implode(", ", $MXHOSTS).")n");
return FALSE;
}
function smtp_message($header, $body){
fputs($this->sock, $header."rn".$body);
$this->smtp_debug("> ".str_replace("rn" , "n"."> ", $header."n> ".$body."n> "));
return TRUE;
}
function 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");
return FALSE;
}
return TRUE;
}
function 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();
}
function smtp_error($string){
$ this->log_write("Error: ".$string.".n");
return FALSE;
}
function 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);
return TRUE;
}
functionstrip_comment($address) {//去除"()"
$comment = "([^()]*)";
while (ereg($comment, $address)){
$address = ereg_replace($comment, "", $address );
}
return $address;
}
function get_address($address){
$address = ereg_replace("([trn])+", "", $address);//t 制表符 r 回车符 n 换行符 +一致前面の子表达式一次または複数次
$address = ereg_replace("^.*<(.+)>.*$", "1", $address);
return $ address;
}
function smtp_debug($message){
if ($this->debug){
echo $message;
}
}
}