PHP示例演示發(fā)送郵件給某個(gè)郵箱
首先,建立一個(gè)index.html文件,代碼如下:
<html> <head> <title>Simple Send Mail </title> </head> <body> <h1>Mail Form</h1> <form name="form1" method="post" action="mail.php"> <table> <tr><td><b>To</b></td><td> <input type="text" name="mailto" size="35"> </td></tr> <tr><td><b>Subject</b></td> <td><input type="text" name="mailsubject" size="35"></td> </tr> <tr><td><b>Message</b></td> <td> <textarea name="mailbody" cols="50" rows="7"></textarea> </td> </tr> <tr><td colspan="2"> <input type="submit" name="Submit" value="Send"> </td> </tr> </table> </form> </body> </html>
然后新建一個(gè)“mail.php”文檔把傳輸?shù)奈臋n進(jìn)行發(fā)送
<?php
$stm="郵件內(nèi)容";
require("smtp.php");
##########################################
$smtpserver = "smtp.qq.com";//SMTP服務(wù)器
$smtpserverport = "465";//SMTP服務(wù)器端口
$smtpusermail = "XXX@qq.com";//SMTP服務(wù)器的用戶郵箱
$smtpemailto = "AAA@qq.com";//發(fā)送給誰(shuí)
$smtpuser = "XXX@qq.com";//SMTP服務(wù)器的用戶帳號(hào)
$smtppass = "666";//SMTP服務(wù)器的用戶密碼
$mailsubject = "666 ";//郵件主題
$mailbody = $stm;//郵件內(nèi)容
$mailtype = "HTML";//郵件格式(HTML/TXT),TXT為文本郵件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這里面的一個(gè)true是表示使用身份驗(yàn)證,否則不使用身份驗(yàn)證.
$smtp->debug = TRUE;//是否顯示發(fā)送的調(diào)試信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
echo "<script>alert('郵件發(fā)送成功');parent.document.ADDUser.cheheh.click();</script>";
exit;
}
?>
最后編寫(xiě)一個(gè)郵件類“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;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = 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("(^|(\r\n))(\.)", "\1.\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML")
{
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "")
{
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$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("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
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;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo))
{
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("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass)))
{
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
{
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
{
return $this->smtp_error("sending RCPT TO command");
}
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("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT"))
{
return $this->smtp_error("sending QUIT command");
}
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("Trying to ".$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("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $MXHOSTS))
{
$this->log_write("Error: Cannot resolve 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("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response))
{
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$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."\r\n");
$this->smtp_debug("> ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$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("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address))
{
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug)
{
//echo $message;
}
}
}
?>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
PHP無(wú)限循環(huán)獲取MySQL中的數(shù)據(jù)實(shí)例代碼
最近公司有個(gè)需求需要從MySQL獲取數(shù)據(jù),然后在頁(yè)面上無(wú)線循環(huán)的翻頁(yè)展示.其實(shí)這個(gè)功能可以通過(guò)jq實(shí)現(xiàn),也可以通過(guò)php+mysql實(shí)現(xiàn),下面小編給大家分享基于PHP無(wú)限循環(huán)獲取MySQL中的數(shù)據(jù)實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2017-08-08
ThinkPHP連接數(shù)據(jù)庫(kù)操作示例【基于DSN方式和數(shù)組傳參的方式】
這篇文章主要介紹了ThinkPHP連接數(shù)據(jù)庫(kù)操作,結(jié)合實(shí)例形式分析了thinkPHP基于DSN方式和數(shù)組傳參的方式進(jìn)行數(shù)據(jù)庫(kù)連接的實(shí)現(xiàn)步驟與屬性設(shè)置、控制器、模板使用等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
php如何計(jì)算兩坐標(biāo)點(diǎn)之間的距離
這篇文章主要為大家詳細(xì)介紹了php如何計(jì)算兩坐標(biāo)點(diǎn)之間的距離,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫(kù)備份功能示例
這篇文章主要介紹了TP5框架實(shí)現(xiàn)的數(shù)據(jù)庫(kù)備份功能,結(jié)合實(shí)例形式分析了TP5數(shù)據(jù)庫(kù)備份功能相關(guān)原理及實(shí)現(xiàn)方法,需要的朋友可以參考下2020-04-04
PHP實(shí)現(xiàn)騰訊與百度坐標(biāo)轉(zhuǎn)換
下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)騰訊與百度坐標(biāo)轉(zhuǎn)換。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
PHP使用流包裝器實(shí)現(xiàn)WebShell的方法
這篇文章主要介紹了PHP使用流包裝器實(shí)現(xiàn)WebShell的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
基于PHP實(shí)現(xiàn)生成隨機(jī)水印圖片
這篇文章主要介紹了基于PHP實(shí)現(xiàn)生成隨機(jī)水印圖片,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
PHP寫(xiě)微信公眾號(hào)文章頁(yè)采集方法
給大家分析一下如何用PHP寫(xiě)出采集微信公眾號(hào)文章的方法以及代碼詳細(xì)講解,需要的朋友學(xué)習(xí)一下。2017-12-12
ThinkPHP靜態(tài)緩存簡(jiǎn)單配置和使用方法詳解
這篇文章主要介紹了ThinkPHP靜態(tài)緩存簡(jiǎn)單配置和使用方法,結(jié)合實(shí)例形式詳細(xì)分析了ThinkPHP靜態(tài)緩存簡(jiǎn)單配置方法,常用參數(shù)含義與相關(guān)使用技巧,需要的朋友可以參考下2016-03-03

