smtp.php
複製程式碼 程式碼如下:
class smtp
{
/* 公用變數 */
var 🎜 > var $log_file;
var $relay_host;
var $debug;
var $auth /* 私有變數*/
var $sock;
/* 建構器*/
function smtp($relay_host {
$this->debug =錯誤的;
$this->smtp_port = $smtp_port;
30; // 用於fsockopen( )
$this->auth = $auth;//auth
$this- $this->host_name =“ localhost”; // 用於HELO 指令
$this->log_file = "";
函數sendmail ($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = " ", $additional_headers =c rom = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(.)", "1.3", $body) ; header .= 「MIME 版本:1.0rn」;
if($mailtype=="HTML")
htmlrn";
}
$header .= 「收件者:」.$to.「rn」;
if ($cc $header .= "抄送: ".$ cc."rn";
}
$header .= "來自: $fromrn";
$header .= “主題:”.$subject.“rn ”;
$header .= $additional_headers;
$header .= .date("rr).P.p. -Mailer:來自Redhat ( PHP/".phpversion().")rn";
list($msec, $sec) = explode(" ", microtime()); rn";
$TO = explode(",", $TO = explode(",", $TO = $to));
if ($cc != "")
$this->strip_comment($cc)));
}
if ($bcc != "")
{1
->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to) get_address($rcpt_to);
if (!$this->smtp_sockopen( $rcpt_to))
{
$sent = FALSE;
則續中;
}
if ($this->smtp, {
{ $this->log_write( "電子郵件已寄至n");
}
$this->log_write("錯誤:無法傳送電子郵件至n 」);
$sent = FALSE;
);
$this->log_write("與遠端主機斷開連接");
}
上與$ 寄送;
}
/*化$509999995 {
if (!$this- >smtp_putcmd("HELO", $helo))
指令");
}
# auth
if($this->auth)
{
> user)))
{
return $this->smtp_error("發送HELO 指令");
}
🎜> {
return $this ->smtp_error("正在傳送HELO 指令」);
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:")) }
if (!$this->smtp_putcmd("RCPT", return $this-> ; smtp_error(「寄RCPT TO 指令」);
}
if (!$this-> return $this->smtp_error("傳送DATA 指令");
}
if (!$this->smtp_message($header, $ return $this->smtp_error("正在傳送訊息");
}
if (!$this->smtp_eom())
{
[EOM] ");
}
if (!$this->smtp_putcmd("QUIT")) return $this->smtp_error("傳送退出指令");
}
返回TRUE;
}
function smtp_sockopenopen($address) {
return $this->s mtp_sockopen_mx($地址);
}
else
>smtp_sockopen_relay();
}
}
function tp) 🎜> $this->log_write("正在嘗試 ".$this->relay_host.":".$this->; smtp_埠。 「n」);
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
this->sock && $this->smtp_ok()))
{
;
$this->log_write("錯誤: ".$errstr." (".$errno.")n"); $this->log_write( "已連線至中繼主機".$this->relay_host."n");
返回TRUE;;
}
函數smtp_sockopen_mx($address)
{
$domain = er if (! @getmxrr($domain, $MXHOSTS))
{
返回假;
}
foreach ($MXHOSTS as $host)
{ port."n 」);
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); ()))
{
$this->log_write("錯誤: " .$errstr." (".$errno.")n");
續;
續;
$this->log_write("已連線至mx 主機".$host."n") ;
返回TRUE;
}
;
返回假;
}
函數smtp_message($header, $body)
;
$ this->smtp_debug(">".str_replace("rn", "n"."> ", $header."n> ".$body."n>"));
}
函數 smtp_eom()
{
fputs($this-> ");
return $this->smtp_ok();
}
函數 place("rn", "", fgets($this-> sock, 512));
$this->smtp_debug($response."n");
{
fputs ($this->sock, "QUITrn");
fgets($this->sock, 512);
");
返回假;
}
function smtp_putcmd($cmd, $arg = "")
{
🎜> if($cmd=="")
{
$cmd = $arg;
{
$cmd = $cmd. }
fputs( $this->sock, $cmd."rn");
$this->smtp_debug("> ".$cmd."n");
🎜> }
函數 smtp_error($string)
{
返回假;
}
函數log_write($message)
{
log_file == "")
{
return TRUE;
}
."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
警告:無法開啟日誌檔案"".$this->log_file.""n");
返回FALSE;;
🎜> fputs($fp, $message);
fclose($fp);
返回TRUE;
> $comment = "([^()]* )";
while (ereg($comment, $address))
{ }
回復$位址中;
}
函數get_address($address)
{
$address = ereg_replace(" ^.*.*$", "1", $address);
與$位址;
}
if ($this->debug)
{
echo $)s
測試程序
複製程式碼
程式碼如下:
/*
這是一個測試程式!!!
*/
require("smtp.php");
##### #####################################
$smtpserver = "smtp.163.com" ;//SMTP伺服器
$smtpserverport = 25;//SMTP伺服器連接埠
$smtpusermail = "reterry123@163.com";//SMTP伺服器的使用者信箱
$smtmailto =gmail. com";//寄給誰
$smtpuser = "reterry123";//SMTP伺服器的使用者帳號
$smtppass = "014040001";//SMTP伺服器的使用者密碼
$subsubject ";//郵件主題
$mailbody = "
以上就介紹了smtown in paris php中通過smtp發郵件的類,測試通過,包括了smtown in paris方面的內容,希望對PHP教程有興趣的朋友有所幫助。