Home >Backend Development >PHP Tutorial >Solution to SMTP Error Could not connect to SMTP host. send fail when sending emails_PHP Tutorial
(1) The server cannot use smtp to send emails
Solution: The solutions listed on many websites say that it is because of the SMTP case problem. Although the essence of the problem is not here, it does need to be changed. As for why, see the operation below.
In class.phpmailer.php, place:
function IsSMTP(){$this->Mailer='smtp';}
changed to:
function IsSMTP(){$this->Mailer = 'SMTP';}
The modification here is not to use smtp to send emails, but to use another way to send emails. Check the class.phpmailer.php file for the following paragraph:
switch($this->Mailer){ case 'sendmail': return $this->SendmailSend($header, $body); case 'smtp'://由于SMTP和smtp不相等 所以选择的是下面MailSend发送邮件 并不是使用smtp发送邮件 return $this->SmtpSend($header, $body); default: return $this->MailSend($header, $body); }
(2) The Linux host has disabled the fsockopen() function
Many space service providers in China disable the fsockopen function of the server for security reasons.
Solution:
Use the pfsockopen() function instead of fsockopen(). If the pfsockopen function is also disabled, you can also replace it with other functions that can operate Socket, such as stream_socket_client()
Change @fsockopen to @pfsockopen in class.smtp.php
Put
$this->smtp_conn = @fsockopen($host, // the host of the server $port, // the port to use $errno, // error number if any $errstr, // error message if any $tval); // give up after ? secs
changed to:
$this->smtp_conn = @pfsockopen($host, // the host of the server $port, // the port to use $errno, // error number if any $errstr, // error message if any $tval); // give up after ? secs
(3) Firewall security setting rules. If the above two solutions do not work, it is probably a problem with the firewall rules. You can ask the server administrator to remove all firewall rules and then test it to see if That's the reason.