① 請問一下,php配置SMTP怎麼弄
PHPMailer的獲取:
PHPMailer項目地址:PHPMailer 使用git命令克隆到本地,或直接在該項目頁面的右下方點擊「 Download ZIP 」即可獲取到完整的PHPMailer代碼包,再到本地解壓即可。
步驟一:使我們的QQ郵箱能夠發送郵件
這里怎麼說能夠發送郵件呢?其實我們的郵箱都是可以發送郵件的,但是要實現在我們的網站中發送郵件,那就要設置一下我們的QQ郵箱了,因為此時我們的網站現在是作為一個第三方客戶端存在的。
這里怎麼說能夠發送郵件呢?其實我們的郵箱都是可以發送郵件的,但是要實現在我們的網站中發送郵件,那就要設置一下我們的QQ郵箱了,因為此時我們的網站現在是作為一個第三方客戶端存在的
由於待會我們用到的是SMTP伺服器來發送,在這里建議把前面的兩項開啟了!當你點擊開啟的時候,它會提示:
<?phprequire_once("./functions.php");$flag=sendMail('[email protected]','lsgo在線通知','恭喜你成功加入LSGO實驗室,開啟你的學習之旅吧!');if($flag){echo"發送郵件成功!";
}else{echo"發送郵件失敗!";
}?>
② phpemail中沒有class.smtp.php啊,怎麼引入
你好,php中引入方式有兩種
1、用include,require直接在文件頭部引入
2、藉助框架中的自帶函數引入,如thinkPHP5,可以直接用use
希望對你有幫助!
③ php發送郵件代碼要最新的網上試了很多都不成功
親,很高興為你解答:
首先,在PHP中可以使用SMTP或內置mail()函數來發送郵件。前者的好處是投遞率高、被認定為垃圾郵件的幾率小、但需要有SMTP伺服器;後者是無需配置,但極易被認定為垃圾郵件。
第一種方法:SMTP:
增加一個SMTP類,命名為class.smtp.php:
<?php
classsmtp
{
/*PublicVariables*/
var$smtp_port;
var$time_out;
var$host_name;
var$log_file;
var$relay_host;
var$debug;
var$auth;
var$user;
var$pass;
/*PrivateVariables*/
var$sock;
/*Constractor*/
functionsmtp($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;//isusedinfsockopen()
$this->auth=$auth;//auth
$this->user=$user;
$this->pass=$pass;
$this->host_name="localhost";//isusedinHELOcommand
$this->log_file="";
$this->sock=FALSE;
}
/*MainFunction*/
functionsendmail($to,$from,$subject="",$body="",$mailtype,$cc="",$bcc="",$additional_headers="")
{
$mail_from=$this->get_address($this->strip_comment($from));
$body=ereg_replace("(^|( ))(.)","1.3",$body);
$header.="MIME-Version:1.0 ";
if($mailtype=="HTML")
{
$header.="Content-Type:text/html ";
}
$header.="To:".$to." ";
if($cc!="")
{
$header.="Cc:".$cc." ";
}
$header.="From:$from<".$from."> ";
$header.="Subject:".$subject." ";
$header.=$additional_headers;
$header.="Date:".date("r")." ";
$header.="X-Mailer:ByRedhat(PHP/".phpversion().") ";
list($msec,$sec)=explode("",microtime());
$header.="Message-ID:<".date("YmdHis",$sec).".".($msec*1000000).".".$mail_from."> ";
$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($TOas$rcpt_to)
{
$rcpt_to=$this->get_address($rcpt_to);
if(!$this->smtp_sockopen($rcpt_to))
{
$this->log_write("Error:Cannotsendemailto".$rcpt_to." ");
$sent=FALSE;
continue;
}
if($this->smtp_send($this->host_name,$mail_from,$rcpt_to,$header,$body))
{
$this->log_write("E-mailhasbeensentto<".$rcpt_to."> ");
}
else
{
$this->log_write("Error:Cannotsendemailto<".$rcpt_to."> ");
$sent=FALSE;
}
fclose($this->sock);
$this->log_write("Disconnectedfromremotehost ");
}
return$sent;
}
/*PrivateFunctions*/
functionsmtp_send($helo,$from,$to,$header,$body="")
{
if(!$this->smtp_putcmd("HELO",$helo))
{
return$this->smtp_error("sendingHELOcommand");
}
#auth
if($this->auth)
{
if(!$this->smtp_putcmd("AUTHLOGIN",base64_encode($this->user)))
{
return$this->smtp_error("sendingHELOcommand");
}
if(!$this->smtp_putcmd("",base64_encode($this->pass)))
{
return$this->smtp_error("sendingHELOcommand");
}
}
if(!$this->smtp_putcmd("MAIL","FROM:<".$from.">"))
{
return$this->smtp_error("sendingMAILFROMcommand");
}
if(!$this->smtp_putcmd("RCPT","TO:<".$to.">"))
{
return$this->smtp_error("sendingRCPTTOcommand");
}
if(!$this->smtp_putcmd("DATA"))
{
return$this->smtp_error("sendingDATAcommand");
}
if(!$this->smtp_message($header,$body))
{
return$this->smtp_error("sendingmessage");
}
if(!$this->smtp_eom())
{
return$this->smtp_error("sending<CR><LF>.<CR><LF>[EOM]");
}
if(!$this->smtp_putcmd("QUIT"))
{
return$this->smtp_error("sendingQUITcommand");
}
returnTRUE;
}
functionsmtp_sockopen($address)
{
if($this->relay_host=="")
{
return$this->smtp_sockopen_mx($address);
}
else
{
return$this->smtp_sockopen_relay();
}
}
functionsmtp_sockopen_relay()
{
$this->log_write("Tryingto".$this->relay_host.":".$this->smtp_port." ");
$this->sock=@fsockopen($this->relay_host,$this->smtp_port,$errno,$errstr,$this->time_out);
if(!($this->sock&&$this->smtp_ok()))
{
$this->log_write("Error:Cannotconnencttorelayhost".$this->relay_host." ");
$this->log_write("Error:".$errstr."(".$errno.") ");
returnFALSE;
}
$this->log_write("Connectedtorelayhost".$this->relay_host." ");
returnTRUE;;
}
functionsmtp_sockopen_mx($address)
{
$domain=ereg_replace("^.+@([^@]+)$","1",$address);
if(!@getmxrr($domain,$MXHOSTS))
{
$this->log_write("Error:CannotresolveMX"".$domain."" ");
returnFALSE;
}
foreach($MXHOSTSas$host)
{
$this->log_write("Tryingto".$host.":".$this->smtp_port." ");
$this->sock=@fsockopen($host,$this->smtp_port,$errno,$errstr,$this->time_out);
if(!($this->sock&&$this->smtp_ok()))
{
$this->log_write("Warning:Cannotconnecttomxhost".$host." ");
$this->log_write("Error:".$errstr."(".$errno.") ");
continue;
}
$this->log_write("Connectedtomxhost".$host." ");
returnTRUE;
}
$this->log_write("Error:Cannotconnecttoanymxhosts(".implode(",",$MXHOSTS).") ");
returnFALSE;
}
functionsmtp_message($header,$body)
{
fputs($this->sock,$header." ".$body);
$this->smtp_debug(">".str_replace(" "," ".">",$header." >".$body." >"));
returnTRUE;
}
functionsmtp_eom()
{
fputs($this->sock," . ");
$this->smtp_debug(".[EOM] ");
return$this->smtp_ok();
}
functionsmtp_ok()
{
$response=str_replace(" ","",fgets($this->sock,512));
$this->smtp_debug($response." ");
if(!ereg("^[23]",$response))
{
fputs($this->sock,"QUIT ");
fgets($this->sock,512);
$this->log_write("Error:Remotehostreturned"".$response."" ");
returnFALSE;
}
returnTRUE;
}
functionsmtp_putcmd($cmd,$arg="")
{
if($arg!="")
{
if($cmd=="")
{
$cmd=$arg;
}
else
{
$cmd=$cmd."".$arg;
}
}
fputs($this->sock,$cmd." ");
$this->smtp_debug(">".$cmd." ");
return$this->smtp_ok();
}
functionsmtp_error($string)
{
$this->log_write("Error:Erroroccurredwhile".$string.". ");
returnFALSE;
}
functionlog_write($message)
{
$this->smtp_debug($message);
if($this->log_file=="")
{
returnTRUE;
}
$message=date("MdH:i:s").get_current_user()."[".getmypid()."]:".$message;
if(!@file_exists($this->log_file)||!($fp=@fopen($this->log_file,"a")))
{
$this->smtp_debug("Warning:Cannotopenlogfile"".$this->log_file."" ");
returnFALSE;;
}
flock($fp,LOCK_EX);
fputs($fp,$message);
fclose($fp);
returnTRUE;
}
functionstrip_comment($address)
{
$comment="([^()]*)";
while(ereg($comment,$address))
{
$address=ereg_replace($comment,"",$address);
}
return$address;
}
functionget_address($address)
{
$address=ereg_replace("([ ])+","",$address);
$address=ereg_replace("^.*<(.+)>.*$","1",$address);
return$address;
}
functionsmtp_debug($message)
{
if($this->debug)
{
echo$message;
}
}
}
?>
然後在PHP裡面引用該文件後,這樣調用發送郵件:
<?php
require_once("class.smtp.php");
$smtpserver="";//SMTP伺服器
$smtpserverport=25;//SMTP伺服器埠
$smtpusermail="";//SMTP伺服器的用戶郵箱
$smtpemailto="";//發送給誰
$smtpuser="";//SMTP伺服器的用戶帳號
$smtppass="";//SMTP伺服器的用戶密碼
$mailsubject="";//郵件主題
$mailbody="";//郵件內容
$mailtype="HTML";//郵件格式(HTML/TXT)
$smtp=newsmtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裡面的一個true是表示使用身份驗證,否則不使用身份驗證
$smtp->debug=TRUE;//是否顯示發送的調試信息
$smtp->sendmail($smtpemailto,$smtpusermail,$mailsubject,$mailbody,$mailtype);
?>
第二種方法:mail()函數
<?php
$to="[email protected],[email protected]";
$subject="HTMLemail";
$message="
<html>
<head>
<title>HTMLemail</title>
</head>
<body>
<p>ThisemailcontainsHTMLTags!</p>
</body>
</html>
";
$headers="MIME-Version:1.0"." ";
$headers.="Content-type:text/html;charset=iso-8859-1"." ";
//其他報頭
$headers.='From:<[email protected]>'." ";
$headers.='Cc:[email protected]'." ";
mail($to,$subject,$message,$headers);
?>
-----------------------------------
如有疑問歡迎追問!
滿意請點擊右上方【選為滿意回答】按鈕 o(∩_∩)o
④ php用smtp 發送郵件失敗提示無法連接到主機
PHPMailer是一個郵件發送插件有很多朋友使用它來發郵件,但也有不少朋友在使用期PHPMailer發郵件時就碰到」SMTP 錯誤:無法連接到 SMTP 主機「錯誤了,出現這種問題我們從幾個點來分享,一個是郵箱配置有問題,另一個是我們的php.ini環境中有些函數沒開啟導致的,下面我來給各位詳細介紹一下問題的排除技巧。
方法2:使用stream_socket_client函數
一般fsockopen()被禁,pfsockopen也有可能被禁,所以這里介紹另一個函數stream_socket_client()。
stream_socket_client的參數與fsockopen有所不同,所以代碼要修改為:
$this->smtp_conn = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $tval);
這樣就可以了。
⑤ 如何使用php通過smtp發送郵件步驟
由於php沒有提供現成的smtp函數,卻提供了一個功能不甚靈活的mail()函數,這個函數需要伺服器配置上的支持,並且不支持smtp驗證,在很多場合無法正常的工作,因此不建議使用。
首先是使用telnet來連接本地的25埠,稍微熟悉點網路的人都知道smtp協議使用25埠,這也就是說,現在在連接本地的smtp伺服器。
<?php
require_once'Mail.php';
$conf['mail']=array(
'host'=>'xx.xx.xx.xx',//smtp伺服器地址,可以用ip地址或者域名
'auth'=>true,//true表示smtp伺服器需要驗證,false代碼不需要
'username'=>'tester',//用戶名
'password'=>'retset'//密碼
);
/***
*使用$headers數組,可以定義郵件頭的內容,比如使用$headers['Reply-To']可以定義回復地址
*通過這種方式,可以很方便的定製待發送郵件的郵件頭
***/
$headers['From']='[email protected]';//發信地址
$headers['To']='[email protected]';//收信地址
$headers['Subject']='testmailsendbyphp';//郵件標題
$mail_object=&Mail::factory('smtp',$conf['mail']);
$body=<<<MSG//郵件正文
helloworld!!!
MSG;
$mail_res=$mail_object->send($headers['To'],$headers,$body);//發送
if(Mail::isError($mail_res)){//檢測錯誤
die($mail_res->getMessage());
}
?>
⑥ 使用php 的 smtp.class.php 發郵件。問題 SMTP Error: MAIL not accepted from server.
有一個PASSWORD的參數,不是郵箱的密碼,而是授權碼,去郵箱的設置里打開SMTP,設置一個授權碼,然後PASSWORD的參數寫入這個授權碼,就可以使用了
⑦ php發送郵件的服務類出現錯誤smtp_connect_failed,怎麼解決
郵件伺服器連接失敗,仔細檢查你發郵件的郵箱地址和密碼是否正確,檢查郵箱是否開通STMP
⑧ php郵件發送類smtp.class.php在伺服器上發送失敗
開啟php配置文件的兩個擴展:extension=php_sockets.dll和extension=php_openssl.dll,將前面的兩個分號去掉就行。
這兩個函數呢 然後重啟服務
⑨ php郵件類報錯Language string failed to load: smtp_connect_failed
那肯定是因為伺服器上配置和本地不一樣。你檢查下伺服器上curl擴展是否打開?
還有,我使用class.phpmailer.php,從來不載入require("class.smtp.php");
⑩ PHP項目使用smtp類,如何設置發件人名稱
下載一個PHPMailer來用吧,下面是代碼:
require_once "/phpmailer/class.phpmailer.php";
// 實例化 PHPMailer 類
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Subject = "XXX"; // 郵件主題
$mail->Body = $message_body; //郵件內容
$mail->Host = "smtp.163.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username"; // SMTP username 注意:普通郵件認證不需要加 @域名
$mail->Password = "password"; // SMTP password
$mail->From = "[email protected]"; // 發件人郵箱
$mail->FromName = "發件人"; // 發件人
$mail->CharSet = "UTF-8"; // 這里指定字元集
$mail->Encoding = "base64";
$mail->IsHTML(true); // send as HTML
// 填入最基本的參數
$mail->AddAddress( "[email protected]" ); // 收件人
$mail->Send();