PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题),PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)
【 tulaoshi.com - PHP 】
smtp.php
<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
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; //is used in fsockopen()
#
$this-auth = $auth;//auth
$this-user = $user;
$this-pass = $pass;
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)#
$this-host_name = "localhost"; //is used in HELO command
$this-log_file = "";
$this-sock = FALSE;
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)/* 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-Version:1.0rn";
if($mailtype=="HTML"){
$header .= "Content-Type:text/htmlrn";
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)$header .= "To: ".$to."rn";
if ($cc != "") {
$header .= "Cc: ".$cc."rn";
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)$header .= "From: $from<".$from."rn";
$header .= "Subject: ".$subject."rn";
$header .= $additional_headers;
$header .= "Date: ".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)));
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if ($bcc != "") {
$TO = array_merge($TO, explode(",", $this-strip_comment($bcc)));
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)$sent = TRUE;
foreach ($TO as $rcpt_to) {
$rcpt_to = $this-get_address($rcpt_to);
if (!$this-smtp_sockopen($rcpt_to)) {
$this-log_write("Error: Cannot send email to ".$rcpt_to."n");
$sent = FALSE;
continue;
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if ($this-smtp_send($this-host_name, $mail_from, $rcpt_to, $header, $body)) {
$this-log_write("E-mail has been sent to <".$rcpt_to."n");
} else {
$this-log_write("Error: Cannot send email to <".$rcpt_to."n");
$sent = FALSE;
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)fclose($this-sock);
$this-log_write("Disconnected from remote hostn");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)return $sent;
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this-smtp_putcmd("HELO", $helo)) {
return $this-smtp_error("sending HELO command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)#auth
if($this-auth){
if (!$this-smtp_putcmd("AUTH LOGIN", base64_encode($this-user))) {
return $this-smtp_error("sending HELO command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_putcmd("", base64_encode($this-pass))) {
return $this-smtp_error("sending HELO command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)#
if (!$this-smtp_putcmd("MAIL", "FROM:<".$from."")) {
return $this-smtp_error("sending MAIL FROM command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_putcmd("RCPT", "TO:<".$to."")) {
return $this-smtp_error("sending RCPT TO command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_putcmd("DATA")) {
return $this-smtp_error("sending DATA command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_message($header, $body)) {
return $this-smtp_error("sending message");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_eom()) {
return $this-smtp_error("sending <CR<LF.<CR<LF [EOM]");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)if (!$this-smtp_putcmd("QUIT")) {
return $this-smtp_error("sending QUIT command");
}
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/php/)return TRUE;
}
来源:http://www.tulaoshi.com/n/20160129/1495049.html
看过《PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)》的人还看了以下文章 更多>>