① 请问一下,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();