轻量级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;
#
$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(“(^|(rn))(\.)”,
“[url=file://\1.\3]\1.\3[/url]”, $body);
$header .= “MIME-Version:1.0rn”;
if($mailtype==”HTML”){
$header .= “Content-Type:text/htmlrn “;
}
$header .= “To: “.$to.”rn”;
if ($cc != “”) {
$header .= “Cc: “.$cc.”rn”;
}
$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)));
}
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 hostn”);
}
echo “<br>”;
echo $header;
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_php使用smtp发送支持附属类小部件的邮件示例,发送邮件代码程序。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(“[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]”,
“[url=file://\1]\1[/url]”, $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.”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(“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.”rn”);
$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 = “[url=file://\([^()]*\]\([^()]*\[/url])”;
while (ereg($comment, $address)) {
$address = ereg_replace($comment, “”, $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace(“([ trn])+”, “”, $address);
$address = ereg_replace(“^.*<(.+)>.*$”,
“[url=file://\1]\1[/url]”, $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message.”<br>”;
}
}
function get_attach_type($image_tag) { //
$filedata = array();
$img_file_con=fopen($image_tag,”r”);
unset($image_data);
while
($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);
$filedata[‘context’] = $image_data;
$filedata[‘filename’]= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,”.”),strlen($image_tag)-strrpos($image_tag,”.”));
switch($extension){
case “.gif”:
$filedata[‘type’] = “image/gif”;
break;
case “.gz”:
$filedata[‘type’] = “application/x-gzip”;
break;
case “.htm”:
$filedata[‘type’] = “text/html”;
break;
case “.html”:
$filedata[‘type’] = “text/html”;
break;
case “.jpg”:
$filedata[‘type’] = “image/jpeg”;
break;
case “.tar”:
$filedata[‘type’] = “application/x-tar”;
break;
case “.txt”:
$filedata[‘type’] = “text/plain”;
break;
case “.zip”:
$filedata[‘type’] = “application/zip”;
break;
default:
$filedata[‘type’] = “application/octet-stream”;
break;
}
成效供给
复制代码 代码如下:
<?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(“(^|(rn))(\.)”,
“[url=file://\1.\3]\1.\3[/url]”, $body);
$header .= “MIME-Version:1.0rn”;
if($mailtype==”HTML”){
$header .= “Content-Type:text/htmlrn “;
}
$header .= “To: “.$to.”rn”;
if ($cc != “”) {
$header .= “Cc: “.$cc.”rn”;
}
$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)));
}
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 hostn”);
}
echo “<br>”;
echo $header;
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(“[url=mailto:^.+@([^@]+)$]^.+@([^@]+)$[/url]”,
“[url=file://\1]\1[/url]”, $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.”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(“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.”rn”);
$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-%3Elog_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 = “[url=file://\([^()]*\]\([^()]*\[/url])”;
while (ereg($comment, $address)) {
$address = ereg_replace($comment, “”, $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace(“([ trn])+”, “”, $address);
$address = ereg_replace(“^.*<(.+)>.*$”,
“[url=file://\1]\1[/url]”, $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug) {
echo $message.”<br>”;
}
}
function get_attach_type($image_tag) { //
$filedata = array();
$img_file_con=fopen($image_tag,”r”);
unset($image_data);
while
($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);
$filedata[‘context’] = $image_data;
$filedata[‘filename’]= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,”.”),strlen($image_tag)-strrpos($image_tag,”.”));
switch($extension){
case “.gif”:
$filedata[‘type’] = “image/gif”;
break;
case “.gz”:
$filedata[‘type’] = “application/x-gzip”;
break;
case “.htm”:
$filedata[‘type’] = “text/html”;
break;
case “.html”:
$filedata[‘type’] = “text/html”;
break;
case “.jpg”:
$filedata[‘type’] = “image/jpeg”;
break;
case “.tar”:
$filedata[‘type’] = “application/x-tar”;
break;
case “.txt”:
$filedata[‘type’] = “text/plain”;
break;
case “.zip”:
$filedata[‘type’] = “application/zip”;
break;
default:
$filedata[‘type’] = “application/octet-stream”;
break;
}
return $filedata;
}
}
?>
send.php:
$title = $_POST[‘title’];
$Message = $_POST[‘Message’];
$obj_title = iconv(‘UTF-8′,’Shift-JIS’,$title);
$obj_message = iconv(‘UTF-8’, ‘Shift-JIS’,$Message);
$smtpserver =
“smtp.163.com”;//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = “”;//SMTP服务器的用户邮箱
$smtpemailto = “”;//发送给哪个人
$smtpuser = “”; //SMTP服务器的用户帐号
$smtppass = “”;//SMTP服务器的用户密码
$mailsubject = $title;//邮件大旨
$mailbody = $Message ;//邮件内容
$mailtype = “HTML”;//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new
smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//那么些中的多少个true是意味着使用身份验证,不然不采用地方验证.
$smtp->debug = true;//是还是不是出示发送的调节和测量检验音讯
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody,
$mailtype);
PHP程序支付,用户在网址注册,需求用户通过邮件链接激活帐号,当用户注册后(用户音信写入数据库),未有登陆邮箱激活帐号,规定24时辰后活动删除没有激活帐号的用户新闻,完结激活链接过期后,用户还是可以使用该新闻在网址注册
<?php
/*
邮件发送smtp服务
统一smtp服务器,进行邮件发送,版权全体,无法复制
@author:jackbrown;
@qq: 610269963
@time:2011-8-20;
@version:1.0.3;
*/
class smtp{
return $filedata;
}
}
?>
send.php:
$title = $_POST[‘title’];
$Message = $_POST[‘Message’];
$obj_title = iconv(‘UTF-8′,’Shift-JIS’,$title);
$obj_message = iconv(‘UTF-8’, ‘Shift-JIS’,$Message);
$smtpserver =
“smtp.163.com”;//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = “”;//SMTP服务器的用户邮箱
$smtpemailto = “”;//发送给何人
$smtpuser = “”; //SMTP服务器的用户帐号
$smtppass = “”;//SMTP服务器的用户密码
$mailsubject = $title;//邮件主旨
$mailbody = $Message ;//邮件内容
$mailtype = “HTML”;//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new
smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面包车型大巴叁个true是意味使用身份验证,不然不行使地方验证.
$smtp->debug = true;//是或不是出示发送的调节和测验新闻
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody,
$mailtype);
盘算数据表
/*邮件用户名*/
public $mailUser = MAIL_USER;
用户消息表中字段Email很要紧,它能够用来验证用户、找回密码、乃至对网站方来讲能够用来搜聚用户消息进行Email经营发卖,以下是用户新闻表t_user的表结构:
/*邮件密码*/
public $mailPwd = MAIL_PWD;
代码如下
/*邮件服务器地址*/
public $server = MAIL_SMTP_HOST;
CREATE TABLE IF NOT EXISTS `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL COMMENT ‘用户名’,
`password` varchar(32) NOT NULL
COMMENT ‘密码’,
`email` varchar(30) NOT NULL COMMENT ‘邮箱’,
`token` varchar(50) NOT NULL COMMENT ‘帐号激活码’,
`token_exptime` int(10) NOT NULL COMMENT ‘激活码保质期’,
`status` tinyint(1) NOT NULL DEFAULT ‘0’ COMMENT
‘状态,0-未激活,1-已激活’,
`regtime` int(10) NOT NULL COMMENT ‘注册时间’,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/*邮件端口*/
public $port = MAIL_SMTP_PORT;
HTML
public $timeout = MAIL_TIMEOUT;
在页面中放置一个报了名表单,用户能够输入注册信息,包罗用户名、密码和信箱。
/*邮件编码*/
public $charset = MAIL_CHARSET;
代码如下
/*邮件发送者email,用于体现给接收者*/
public $senderMail = MAIL_SENDER;
<form id=”reg” action=”register.php” method=”post”>
<p>用户名:<input type=”text” class=”input” name=”username”
id=”user”></p>
<p>密 码:<input type=”password” class=”input”
name=”password” id=”pass”></p>
<p>E-mail:<input type=”text” class=”input” name=”email”
id=”email”></p>
<p><input type=”submit” class=”btn”
value=”提交注册”></p>
</form>
/*发用者名称*/
public $senderName = MAIL_SENDER_NAME;
对此用户的输入要开始展览要求的前端验证,关于表单验证功能,提议您参照他事他说加以考察本站小说:实例讲利尿单验证插件Validation的利用,本文对后边贰个验证代码略过,另外其实页面中还应有有个要求用户重复输入密码的输入框,有时偷懒就此略过。
register.php
/*是或不是使用ssl安全操作*/
public $useSSL = IN_SSL;
用户将注册音讯交到到register.php举行拍卖。register.php需求形成写入数据和发送邮件两大职能。
/*是还是不是出示错误新闻*/
public $showError = MAIL_SHOW_ERR;
第一包罗要求的八个公文,connect.php和smtp.class.php,那七个公文在外场提供的下载包里有,款待下载。
public $needLogin = MAIL_NEED_LOGIN;
代码如下
/*附属类小部件数组*/
public $attachMent = array();
include_once(“connect.php”);//连接数据库
include_once(“smtp.class.php”);//邮件发送类
public $failed = false;
下一场咱们要过滤用户提交的音讯,并表明用户名是还是不是存在(前端也足以表明)。
private static $smtpCon;
private $stop =”\r\n”;
private $status = 0;
$username =
stripslashes(trim($_POST[‘username’]));
$query = mysql_query(“select
id from t_user where username=’$username'”);
$num = mysql_num_rows($query);
if($num==1){
echo ‘用户名已存在,请换个其余的用户名’;
exit;
}
继而我们将用户密码加密,构造激活识别码:
public function __construct(){
代码如下
if(self::$smtpCon){
return;
}
$password = md5(trim($_POST[‘password’])); //加密密码
$email = trim($_POST[’email’]); //邮箱
$regtime = time();
$token = md5($username.$password.$regtime); //创设用于激活识别码
$token_exptime = time()+60*60*24;//过期时间为24时辰后
$sql = “insert into `t_user`
(`username`,`password`,`澳门葡京备用网址,email`,`token`,`token_exptime`,`regtime`)
values
(‘$username’,’$password’,’$email’,’$token’,’$token_exptime’,’$regtime’)”;
mysql_query($sql);
if($this->mailUser==”){
$this->error(‘请配置好邮件登陆用户名!’);
return false;
}
上述代码中,$token即协会好的激活识别码,它是由用户名、密码和近期时间组成并md5加密得来的。$token_exptime用于安装激活链接UWranglerL的逾期时间,用户在这几个时刻段内能够激活帐号,本例设置的是24小时内激活有效。最终将这么些字段插入到数码表t_user中。
if($this->mailPwd==”){
$this->error(‘请配置好邮件登入密码!’);
return false;
}
当数码插入成功后,调用邮件发送类将激活音信发送给用户注册的邮箱,注意将组织好的激活识别码组成一个整机的U本田UR-VL作为用户点击时的激活链接,以下是事无巨细代码:
if($this->server==”){
$this->error(‘请配置好邮服务器地址!’);
return false;
}
代码如下
if(!is_numeric($this->port)){
$this->error(‘请配置好邮服务器端口!’);
return false;
}
if(mysql_insert_id()){
$smtpserver = “”; //SMTP服务器,如:smtp.163.com
$smtpserverport = 25; //SMTP服务器端口,一般为25
$smtpusermail = “”; //SMTP服务器的用户邮箱,如xxx@163.com
$smtpuser = “”; //SMTP服务器的用户帐号xxx@163.com
$smtppass = “”; //SMTP服务器的用户密码
$smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser,
$smtppass); //实例化邮件类
$emailtype = “HTML”; //信件类型,文本:text;网页:HTML
$smtpemailto = $email; //接收邮件方,本例为注册用户的Email
$smtpemailfrom = $smtpusermail; //发送邮件方,如xxx@163.com
$emailsubject = “用户帐号激活”;//邮件标题
//邮件主体内容
$emailbody =
“亲爱的”.$username.”:<br/>谢谢您在自己站注册了新帐号。<br/>请点击链接激活您的帐号。<br/>
<a href=’/demo/register/active.php?verify=”.$token.”‘ target=
‘_blank’>/demo/register/active.php?verify=”.$token.”</a><br/>
若是上述链接不能点击,请将它复制到你的浏览器地址栏中步向访问,该链接24钟头内一蹴而就。”;
//发送邮件
$rs = $smtp->sendmail($smtpemailto, $smtpemailfrom,
$emailsubject, $emailbody, $emailtype);
if($rs==1){
$msg =
‘恭喜您,注册成功!<br/>请登陆到您的邮箱及时激活你的帐号!’;
}else{
$msg = $rs;
}
}
echo $msg;
/*ssl使用**/
$server = $this->server;
if($this->useSSL == true){
$server = “ssl://”.$this->server;
}
再有贰个一定好用且庞大的邮件发送类分享个大家:使用PHPMailer发送带附属类小部件并帮助HTML内容的邮件,直接能够用哦。
active.php
self::$smtpCon = @fsockopen($server, $this->port, $errno,
$errstr,10);;
若是不出意外,您注册帐号时填写的Email将收受一封helloweba发送的邮件,那一年你一贯点击激活链接,交由active.php管理。
if(!self::$smtpCon){
$this->error($errno.$errstr);
return false;
}
active.php接收提交的链接音讯,获取参数verify的值,即激活识别码。将它与数据表中的用户音讯实行查询相比较,假诺有照料的数据集,推断是不是过期,若是在保质期内则将相应的用户表中字段status设置1,即已激活,那样就到位了激活功用。
socket_set_timeout(self::$smtpCon,0,250000);
代码如下
/*开班邮件指令*/
$this->getStatus();
$resp = true;
$resp = $resp && $this->helo();
if($this->needLogin == ‘1’){
$resp = $resp && $this->login();
}
include_once(“connect.php”);//连接数据库
$verify = stripslashes(trim($_GET[‘verify’]));
$nowtime = time();
$query = mysql_query(“select id,token_exptime from t_user where
status=’0′ and
`token`=’$verify'”);
$row = mysql_fetch_array($query);
if($row){
if($nowtime>$row[‘token_exptime’]){ //24hour
$msg = ‘您的激活保藏期已过,请登陆您的帐号重新发送激活邮件.’;
}else{
mysql_query(“update t_user set status=1 where
id=”.$row[‘id’]);
if(mysql_affected_rows($link)!=1) die(0);
$msg = ‘激活成功!’;
}
}else{
$msg = ‘error.’;
}
echo $msg;
if(!$resp){
$this->failed = true;
}
激活成功后,开采token字段并从未用处了,您能够清空。接下来大家会讲课用户找回密码的遵循,也要用到邮箱验证,敬请关怀。
}
末段附上邮箱smtp.class.php发送类
/*
发送邮件
@param string $to 接收邮件地址
@param string $msg 邮件首要内容
@title string $title 邮件标题
*/
public function sendMail($to,$msg,$title=”){
代码如下
if($msg==” ){
<?php
return false;
}
if(is_array($to)){
class Smtp{
if($to!=null){
foreach($to as $k=>$e){
/* Public Variables */
if(!preg_match(‘/^[a-z0-9A-Z_-]+@+([a-z0-9A-Z_-]+\.)+[a-z0-9A-Z]{2,3}$/’,$e)){
var $smtp_port;
unset($to[$k]);
}
}
}else{
return false;
}
var $time_out;
if($to == null){
return false;
}
var $host_name;
}else{
var $log_file;
if(!preg_match(‘/^[a-z0-9A-Z_-]+@+([a-z0-9A-Z_-]+\.)+[a-z0-9A-Z]{2,3}$/’,$to)){
var $relay_host;
return false;
}
var $debug;
}
var $auth;
if(!self::$smtpCon){
return false;
}
var $user;
$this->sendSmtpMsg(‘MAIL FROM:<‘.$this->senderMail.’>’);
var $pass;
if(!is_array($to)){
$this->sendSmtpMsg(‘RCPT TO:<‘.$to.’>’);
}else{
/* Private Variables */
var $sock;
foreach($to as $k=>$email){
$this->sendSmtpMsg(‘RCPT TO:<‘.$email.’>’);
}
}
/* Constractor */
$this->sendSmtpMsg(“DATA”);
function smtp($relay_host = “”, $smtp_port = 25, $auth = false,
$user, $pass) {
$this->debug = false;
if($this->status !=’354′){
$this->error(‘诉求发送邮件战败!’);
$this->failed = true;
return false;
}
$this->smtp_port = $smtp_port;
$msg = base64_encode($msg);
$msg = str_replace($this->stop . ‘.’, $this->stop . ‘..’,
$msg);
$msg = substr($msg, 0, 1) == ‘.’ ? ‘.’ . $msg : $msg;
$this->relay_host = $relay_host;
if($this->attachMent!=null){
$this->time_out = 30; //is used in fsockopen()
$headers = $this->mimeHeader($msg,$to,$title);
$this->sendSmtpMsg($headers,false);
$this->auth = $auth; //auth
}else{
$this->user = $user;
$headers = $this->mailHeader($to,$title);
$this->sendSmtpMsg($headers,false);
$this->sendSmtpMsg(”,false);
$this->sendSmtpMsg($msg,false);
}
$this->sendSmtpMsg(‘.’);//发送甘休标志符
$this->pass = $pass;
if($this->status != ‘250’){
$this->failed = true;
$this->error($this->readSmtpMsg());
return false;
}
$this->host_name = “localhost”; //is used in HELO command
$this->log_file = “”;
return true;
}
$this->sock = false;
}
/*
关闭邮件连接
*/
public function close(){
/* Main Function */
$this->sendSmtpMsg(‘Quite’);
@socket_close(self::$smtpCon);
}
function sendmail($to, $from, $subject = “”, $body = “”, $mailtype, $cc
= “”, $bcc = “”, $additional_headers = “”) {
$mail_from = $this->get_address($this->strip_comment($from));
/*
增加普通邮件头音信
*/
protected function mailHeader($to,$title){
$headers = array();
$headers[] = ‘Date: ‘.$this->gmtime(‘D j M Y H:i:s’).’
‘.date(‘O’);
$body = ereg_replace(“(^|(rn))(.)”, “1.3”, $body);
if(!is_array($to)){
$headers[] = ‘To:
“‘.’=?’.$this->charset.’?B?’.base64_encode($this->getMailUser($to)).’?=”<‘.$to.’>’;
}else{
foreach($to as $k=>$e){
$headers[] = ‘To:
“‘.’=?’.$this->charset.’?B?’.base64_encode($this->getMailUser($e)).’?=”<‘.$e.’>’;
}
}
$header .= “MIME-Version:1.0rn”;
$headers[] = ‘From:
“=?’.$this->charset.’?B?’.base64_encode($this->senderName).’?=”<‘.$this->senderMail.’>’;
$headers[] = ‘Subject:
=?’.$this->charset.’?B?’.base64_encode($title).’?=’;
$headers[] = ‘Content-type: text/html;
charset=’.$this->charset.’; format=flowed’;
$headers[] = ‘Content-Transfer-Encoding: base64’;
if ($mailtype == “HTML”) {
$header .= “Content-Type:text/htmlrn”;
}
$headers = str_replace($this->stop . ‘.’, $this->stop .
‘..’, trim(implode($this->stop, $headers)));
return $headers;
}
$header .= “To: ” . $to . “rn”;
/*
带付件的头顶消息
*/
protected function mimeHeader($msg,$to,$title){
if ($cc != “”) {
$header .= “Cc: ” . $cc . “rn”;
}
if($this->attachMent!=null){
$header .= “From: $from<” . $from . “>rn”;
$headers = array();
$boundary = ‘—-=’.uniqid();
$headers[] = ‘Date: ‘.$this->gmtime(‘D j M Y H:i:s’).’
‘.date(‘O’);
if(!is_array($to)){
$headers[] = ‘To:
“‘.’=?’.$this->charset.’?B?’.base64_encode($this->getMailUser($to)).’?=”<‘.$to.’>’;
}else{
foreach($to as $k=>$e){
$headers[] = ‘To:
“‘.’=?’.$this->charset.’?B?’.base64_encode($this->getMailUser($e)).’?=”<‘.$e.’>’;
}
}
$header .= “Subject: ” . $subject . “rn”;
$headers[] = ‘From:
“=?’.$this->charset.’?B?’.base64_encode($this->senderName).’?=”<‘.$this->senderMail.’>’;
$headers[] = ‘Subject:
=?’.$this->charset.’?B?’.base64_encode($title).’?=’;
$headers[] = ‘Mime-Version: 1.0’;
$headers[] = ‘Content-Type:
multipart/mixed;boundary=”‘.$boundary.'”‘.$this->stop;
$headers[]=’–‘.$boundary;
$header .= $additional_headers;
$headers[]=’Content-Type:
text/html;charset=”‘.$this->charset.'”‘;
$headers[]=’Content-Transfer-Encoding: base64′.$this->stop;
$headers[] = ”;
$headers[]= $msg.$this->stop;
$header .= “Date: ” . date(“r”) . “rn”;
foreach($this->attachMent as $k=>$filename){
$header .= “X-Mailer:By Redhat (PHP/” . phpversion() . “)rn”;
$f = @fopen($filename, ‘r’);
$mimetype = $this->getMimeType(realpath($filename));
$mimetype = $mimetype == ” ? ‘application/octet-stream’ :
$mimetype;
list ($msec, $sec) =
explode(” “, microtime());
$attachment = @fread($f, filesize($filename));
$attachment = base64_encode($attachment);
$attachment = chunk_split($attachment);
$header .= “Message-ID: <” . date(“YmdHis”, $sec) . “.” . ($msec *
1000000) . “.” . $mail_from . “>rn”;
$headers[] = “–” . $boundary;
$headers[] = “Content-type:
“.$mimetype.”;name=\”=?”.$this->charset.”?B?”.
base64_encode(basename($filename)).’?=”‘ ;
$headers[] = “Content-disposition: attachment;
name=\”=?”.$this->charset.”?B?”.
base64_encode(basename($filename)).’?=”‘;
$headers[] = ‘Content-Transfer-Encoding: base64’.$this->stop;
$headers[] = $attachment.$this->stop;
$TO = explode(“,”, $this->strip_comment($to));
if ($cc != “”) {
$TO = array_merge($TO, explode(“,”,
$this->strip_comment($cc)));
}
}
$headers[] = “–” . $boundary . “–“;
$headers = str_replace($this->stop . ‘.’, $this->stop . ‘..’,
trim(implode($this->stop, $headers)));
return $headers;
if ($bcc != “”) {
$TO = array_merge($TO, explode(“,”,
$this->strip_comment($bcc)));
}
}
}
$sent = true;
/*
获取重临状态
*/
protected function getStatus(){
foreach ($TO as $rcpt_to)
{
$rcpt_to = $this->get_address($rcpt_to);
$this->status = substr($this->readSmtpMsg(),0,3);
}
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write(“Error: Cannot send email to ” . $rcpt_to .
“n”);
/*
获取邮件服务器重临的音讯
@return string 新闻字符串
*/
protected function readSmtpMsg(){
$sent = false;
if(!is_resource(self::$smtpCon)){
return false;
}
continue;
}
$return = ”;
$line = ”;
while (strpos($return, $this->stop)=== false OR $line{3}!== ‘ ‘)
{
$line = fgets(self::$smtpCon, 512);
$return .= $line;
}
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”);
return trim($return);
$sent = false;
}
}
fclose($this->sock);
/*
给邮件服务器发给钦点命令新闻
*/
protected function sendSmtpMsg($cmd,$chStatus=true){
if (is_resource(self::$smtpCon))
{
fwrite(self::$smtpCon, $cmd . $this->stop, strlen($cmd)
$this->log_write(“Disconnected from remote hostn”);
}
- 2);
}
if($chStatus == true){
$this->getStatus();
}
return $sent;
}
return true;
}
/* Private Functions */
/*
邮件时间格式
*/
protected function gmtime(){
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”);
}
return (time() – date(‘Z’));
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”);
}
/*
获取付件的mime类型
*/
protected function getMimeType($file){
if (!$this->smtp_putcmd(“RCPT”, “TO:<” . $to . “>”)) {
return $this->smtp_error(“sending RCPT TO command”);
}
$mimes = array(
‘chm’=>’application/octet-stream’,
‘ppt’=>’application/vnd.ms-powerpoint’,
‘xls’=>’application/vnd.ms-excel’, ‘doc’=>’application/msword’,
‘exe’=>’application/octet-stream’,
‘rar’=>’application/octet-stream’, ‘js’=>”javascrīpt/js”,
‘css’=>”text/css”,
‘hqx’=>”application/mac-binhex40″,
‘bin’=>”application/octet-stream”, ‘oda’=>”application/oda”,
‘pdf’=>”application/pdf”,
‘ai’=>”application/postsrcipt”,
‘eps’=>”application/postsrcipt”, ‘es’=>”application/postsrcipt”,
‘rtf’=>”application/rtf”,
‘mif’=>”application/x-mif”, ‘csh’=>”application/x-csh”,
‘dvi’=>”application/x-dvi”, ‘hdf’=>”application/x-hdf”,
‘nc’=>”application/x-netcdf”, ‘cdf’=>”application/x-netcdf”,
‘latex’=>”application/x-latex”, ‘ts’=>”application/x-troll-ts”,
‘src’=>”application/x-wais-source”, ‘zip’=>”application/zip”,
‘bcpio’=>”application/x-bcpio”, ‘cpio’=>”application/x-cpio”,
‘gtar’=>”application/x-gtar”, ‘shar’=>”application/x-shar”,
‘sv4cpio’=>”application/x-sv4cpio”,
‘sv4crc’=>”application/x-sv4crc”,
‘tar’=>”application/x-tar”,’ustar’=>”application/x-ustar”,’man’=>”application/x-troff-man”,
‘sh’=>”application/x-sh”,
‘tcl’=>”application/x-tcl”, ‘tex’=>”application/x-tex”,
‘texi’=>”application/x-texinfo”,’texinfo’=>”application/x-texinfo”,
‘t’=>”application/x-troff”, ‘tr’=>”application/x-troff”,
‘roff’=>”application/x-troff”,
‘shar’=>”application/x-shar”, ‘me’=>”application/x-troll-me”,
‘ts’=>”application/x-troll-ts”,
‘gif’=>”image/gif”, ‘jpeg’=>”image/pjpeg”,
‘jpg’=>”image/pjpeg”, ‘jpe’=>”image/pjpeg”,
‘ras’=>”image/x-cmu-raster”,
‘pbm’=>”image/x-portable-bitmap”,
‘ppm’=>”image/x-portable-pixmap”, ‘xbm’=>”image/x-xbitmap”,
‘xwd’=>”image/x-xwindowdump”,
‘ief’=>”image/ief”, ‘tif’=>”image/tiff”,
‘tiff’=>”image/tiff”, ‘pnm’=>”image/x-portable-anymap”,
‘pgm’=>”image/x-portable-graymap”,
‘rgb’=>”image/x-rgb”, ‘xpm’=>”image/x-xpixmap”,
‘txt’=>”text/plain”, ‘c’=>”text/plain”, ‘cc’=>”text/plain”,
‘h’=>”text/plain”, ‘html’=>”text/html”, ‘htm’=>”text/html”,
‘htl’=>”text/html”, ‘rtx’=>”text/richtext”,
‘etx’=>”text/x-setext”,
‘tsv’=>”text/tab-separated-values”, ‘mpeg’=>”video/mpeg”,
‘mpg’=>”video/mpeg”, ‘mpe’=>”video/mpeg”,
‘avi’=>”video/x-msvideo”,
‘qt’=>”video/quicktime”, ‘mov’=>”video/quicktime”,
‘moov’=>”video/quicktime”, ‘movie’=>”video/x-sgi-movie”,
‘au’=>”audio/basic”,
‘snd’=>”audio/basic”, ‘wav’=>”audio/x-wav”,
‘aif’=>”audio/x-aiff”, ‘aiff’=>”audio/x-aiff”,
‘aifc’=>”audio/x-aiff”,
‘swf’=>”application/x-shockwave-flash”,
‘myz’=>”application/myz”
);
if (!$this->smtp_putcmd(“DATA”)) {
return $this->smtp_error(“sending DATA command”);
}
$ext = substr(strrchr($file,’.’),1);
$type = $mimes[$ext];
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error(“sending message”);
}
unset($mimes);
return $type;
}
if (!$this->smtp_eom()) {
return $this->smtp_error(“sending
<CR><LF>.<CR><LF> [EOM]”);
}
/*
邮件helo命令
*/
private function helo(){
if (!$this->smtp_putcmd(“QUIT”)) {
return $this->smtp_error(“sending QUIT command”);
}
if($this->status != ‘220’){
return true;
}
$this->error(‘连接服务器退步!’);
return false;
}
function smtp_sockopen($address) {
if ($this->relay_host == “”) {
return $this->smtp_sockopen_mx($address);
} else {
return $this->smtp_sockopen_relay();
}
}
return $this->sendSmtpMsg(‘HELO ‘.$this->server);
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);
/*
登录
*/
private function login(){
if (!($this->sock && $this->smtp_ok())) {
$this->log_write(“Error: Cannot connenct to relay host ” .
$this->relay_host . “n”);
if($this->status!=’250′){
$this->log_write(“Error: ” . $errstr . ” (” . $errno . “)n”);
$this->error(‘helo邮件指令失败!’);
return false;
}
return false;
}
$this->sendSmtpMsg(‘AUTH LOGIN’);
if($this->status!=’334′){
$this->error(‘AUTH LOGIN 邮件指令失利!’);
return false;
}
$this->log_write(“Connected to relay host ” .
$this->relay_host . “n”);
$this->sendSmtpMsg(base64_encode($this->mailUser));
if($this->status!=’334′){
$this->error(‘邮件登入用户名恐怕不得法!’.$this->readSmtpMsg());
return false;
}
return true;
;
}
$this->sendSmtpMsg(base64_encode($this->mailPwd));
if($this->status !=’235′){
$this->error(‘邮件登入密码大概不得法!’);
return false;
}
function smtp_sockopen_mx($address) {
$domain = ereg_replace(“^.+@([^@]+)$”, “1”, $address);
return true;
if (!@ getmxrr($domain, $MXHOSTS)) {
$this->log_write(“Error: Cannot resolve MX “” . $domain . “”n”);
}
return false;
}
private function getMailUser($to){
foreach ($MXHOSTS as $host) {
$this->log_write(“Trying to ” . $host . “:” .
$this->smtp_port . “n”);
$temp = explode(‘@’,$to);
return $temp[0];
}
/*
非凡报告
*/
private function error($exception){
$this->sock = @ fsockopen($host, $this->smtp_port, $errno,
$errstr, $this->time_out);
if($this->showError == false){
file_put_contents(‘mail_log.txt’,$exception,FILE_APPEND);
return;
}
if (!($this->sock && $this->smtp_ok())) {
$this->log_write(“Warning: Cannot connect to mx host ” . $host .
“n”);
if(class_exists(‘error’) && is_object($GLOBALS[‘error’])){
$GLOBALS[‘error’]->showErrorStr($exception,’javascript:’,false);
}else{
throw new Exception($exception);
}
}
$this->log_write(“Error: ” . $errstr . ” (” . $errno . “)n”);
}
continue;
}
// 使用示例
ini_set(‘memory_limit’,’128M’);
set_time_limit(120);
define(‘MAIL_SENDER_NAME’,’楚贤’);
define(‘MAIL_SMTP_HOST’,’smtp.ym.163.com’);
define(‘MAIL_USER’,’admin@myxxxx.com’);
define(‘MAIL_SENDER’,’admin@myxxxx.com’);
define(‘MAIL_PWD’,’xxxx’);
define(‘MAIL_SMTP_PORT’,25);
define(‘IN_SSL’,false);
define(‘MAIL_TIMEOUT’,10);
define(‘MAIL_CHARSET’,’utf-8′);
date_default_timezone_set(‘PRC’);
$m = new smtp();
$msg = “有用户登入服务器@”.date(‘Y-m-d H:i:s’);
付件
//$m->attachMent = array(‘hehe.php’,’common.php’);
if($m->sendMail(array(‘610269963@qq.com’),$msg,’88服务器登入提醒’)){
echo ‘发送成功!’;
}
$m->close();
?>
$this->log_write(“Connected to mx host ” . $host . “n”);
你大概感兴趣的篇章:
- PHPMailer邮件类应用smtp.163.com发送邮件方式
- PHP mail
通过Windows的SMTP发送邮件退步的消除方案 - php中通过curl smtp发送邮件
- php smtp完毕出殡和埋葬邮件作用
- php基于socket达成SMTP发送邮件的艺术
- php
mailer类调用远程SMTP服务器发送邮件达成方式 - php使用pear_smtp发送邮件
- PHP达成协理SSL连接的SMTP邮件发送类
- PHP的叁个平安无事SMTP类(消除邮件服务器须求表明时的标题)
- PHP使用SMTP邮件服务器发送邮件示例
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 . “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(“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 . “rn”);
$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(“([ trn])+”, “”, $address);
$address = ereg_replace(“^.*<(.+)>.*$”, “1”, $address);
return $address;
}
function smtp_debug($message) {
if ($this->debug) {
echo $message . “
;”;
}
}
}
?>
connect数据库连接类
代码如下
<?php
$host=”localhost”;
$db_user=””;//用户名
$db_pass=””;//密码
$db_name=”demo”;//数据库
$timezone=”Asia/Shanghai”;
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query(“SET names UTF8”);
header(“Content-Type: text/html; charset=utf-8”);
date_default_timezone_set($timezone); //法国巴黎时间
?>