Home >Backend Development >PHP Tutorial >PHPMailer cannot send emails, PHPMailer sends emails_PHP Tutorial
PHPMailerCannot connect to the SMTP server, it has nothing to do with changing SMTP case
(2011-10-22 12:17:35)
Reprinted▼
|
Category: Default category |
PHPmailer cannot send emails, prompting the error Error: Could not connect to SMTP host
There are two previous articles on the blog, "PHPMailer:: Cannot connect to SMTP server" "Two common reasons why PHPMailer cannot connect to SMTP server" Reasons》
One is for reprinting and the other is for taking notes. As a result, it misleads people. Not everyone can solve the problem.
A friend wrote to ask for help, and I was also anxious. Although it was solved later, I still couldn’t figure it out. I calmed down and looked at it again
PHPMailer cannot connect to the SMTP server. Why? Check it with the code first:
function Get_host($host){ //Resolve domain name
$Get_host=gethostbyname($host);
echo "Try to connect $host...
rn " ;
if(!$Get_host){
$str= "Parse failed (1)
PHPmailer is a great PHP class for sending mail. Error handling focuses on problems during the session with the SMTP server, such as incorrect authentication and empty recipient error messages, but for connections The error message during the SMTP process is simply "Could not connect to SMTP host", which has led to many problems that have not been solved. What is even more ridiculous is that some useful but unreasonable methods have been spread around the world. It can be seen that there is a mystery. Everything in it has a definite number.
Okay, no more talking.
If you want to understand the reason why Could not connect to SMTP host, you must understand the steps of connecting to the service.
A complete and effective SMTP sending process should include: resolving domain name, connecting to SMTP server, verifying identity, and confirming the recipient. People and letter content, sending
The above PHP code is to separate these steps, find out the reason, and then find a method. The echoed results are probably as follows:
1, resolution failure (2): It may be an invalid host name
indicating that the domain name cannot be resolved. . Might be a DNS level issue. Contact the administrator or change the service provider
2, the server does not support Fsockopen, try the pFsockopen function
If the pfsockopen function is used to connect to the server successfully, modify the class .smtp.php's $this->smtp_conn = fsockopen( is $this->smtp_conn = pfsockopen(. Make PHPmailer resume normal use
3, server-side error
Successfully established a connection with the remote host, but the other party did not install the SMTP protocol and sent a 220 response code, indicating that there may be a problem with the SMTP server
4, 220 SMTP service The response from the client is normal
Well, whether it is the fsockopen function or the pfsockopen function, it has been connected to the remote SMTP server normally. If you are unable to send letters using PHPmailer, I strongly recommend that you change your account and try again.
5, and other errors, such as this
Warning: fsockopen(): unable to connect to smtp163.com:25
You have absolutely reason to believe that the firewall is responsible! In this case, if you cannot contact the administrator to change the firewall rules, you can try the method in "PHPMailer:: Cannot Connect to SMTP Server" and search for
function IsSMTP. () {
$this->Mailer = 'smtp';
}
changed to:
function IsSMTP() {
$this->Mailer = 'SMTP ';
}
As my title says, "PHPMailer cannot connect to the SMTP server, and it has nothing to do with changing the SMTP case." Of course, I can't make fun of you, but it actually works sometimes. , the success rate of cure depends on your character
Let’s analyze the reason.
This code is probably around line 286 of class.phpmailer.php. This function must be called first when using the PHPmailer class. , used to declare the method of sending mail
Trace this->Mailer to about line 400 of class.smtp.php
case 'sendmail':
$result = $this->SendmailSend($header, $body);
break;
case 'smtp':
$result = $this->SmtpSend($header, $body);
break;
case 'mail':
$result = $this->MailSend( $header, $body);
break;
default:
$result = $this->MailSend($header, $body);
break;
So, if the above conditions are not met, PHPmailer will execute
$result = $this->MailSend($header, $body);This sentence
Let’s track MailSend() again The function is around line 460 of class.phpmailer.php:
function MailSend($header, $body) {
$to = '';
for($i = 0; $i < count($this->to); $i++) {
if($i != 0) { $to .= ', '; }
$to .= $this->AddrFormat($this->to[$i]);
}
$toArr = split(',', $to);
$params = sprintf("-oi -f %s", $this->Sender);
if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
$old_from = ini_get('sendmail_from');
ini_set('sendmail_from', $this->Sender);
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
if ($this->SingleTo === true && count($toArr) > 1) {
foreach ($toArr as $key => $val) {
$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
}
} else {
$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
}
}
if (isset($old_from)) {
ini_set('sendmail_from', $old_from);
}
if(!$rt) {
$this->SetError($this->Lang('instantiate'));
return false;
}
return true;
}
注意$rt = @mail( 这是用PHP内置的mail函数发信啊!
来自W3School的mail发信实例
$to = "somebody@example.com"; //这里改成你的邮箱地址
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: dongfangtianyu@qq.com" . "rn" .
mail($to,$subject,$txt,$headers);
?>
如果在你的服务器上运行这脚本能够收到邮件,那么你完全可以用修改SMTP大小写的方法。不过,毕竟不大好用
.
想要使用mail函数函数发信,需要修改设置php.ini,也即是说,成与不成得看你的服务提供商。
如果服务器已经设置好了mail()相关的一切,PHPmailer使用mail()的方法当然可以发信成功。不再依赖fsockopen函数
这也就是为什么防火墙禁止的情况下,用修改smtp大小写反而能用PHPmailer发信,因为那封e-mail根本是用本地的smtp服务器代发的
亲爱的朋友,你明白了吗?