Home >Backend Development >PHP Tutorial >Solution to SMTP Error: Could not connect to SMTP host when phpmailer sends email

Solution to SMTP Error: Could not connect to SMTP host when phpmailer sends email

一个新手
一个新手Original
2017-09-13 09:30:454682browse

When I was working on a project before, I made a function to send emails using phpmailer, "CI Framework Combined with PHPmailer to Send Emails". It was launched yesterday (I just started using Sina Cloud, but it was too expensive, so I switched to Alibaba). When testing, I accidentally reported an error when sending an email...

I wiped it, and it worked fine when I was not online. It succeeded every time. At first I thought it was There was a problem with the smtp address (I used 163 mailbox) and later changed it to QQ mailbox. I found that it still didn't work. I had no choice but to ask Du Niang. Later, after looking at the answers on Baidu, I realized that in addition to Google's smtp server receiving the request "smtp" will be accepted. Other servers are like the 163 I use. QQ and other servers must receive uppercase "smtp" requests... emmmmm....

Then in class.phpmailer.php I put


1 public function IsSMTP() {
2     $this->Mailer = 'smtp';
3   }
4 
5 //改成
6 public function IsSMTP() {
7     $this->Mailer = 'SMTP';
8   }

and then:


 1 switch($this->Mailer) { 
 2         case 'sendmail': 
 3           return $this->SendmailSend($header, $body); 
 4         case 'smtp': 
 5           return $this->SmtpSend($header, $body); 
 6         default: 
 7           return $this->MailSend($header, $body); 
 8       } 
 9 
 10 
 11 //改成
 12 switch($this->Mailer) {
 13         case 'sendmail':
 14           return $this->SendmailSend($header, $body);
 15         case 'SMTP':
 16           return $this->SmtpSend($header, $body);
 17         default:
 18           return $this->MailSend($header, $body);
 19       }

I originally thought this would be enough. I restarted Apache and tested it again. As a result, the first error was solved, and another error appeared:

Could not instantiate mail function

? ? ? ? ?

I don’t know if you guys showed up. I had bad luck, so I had to ask Du Niang for help, and finally found the reason: some virtual hosts or servers blocked the "fsockopen() function" for security reasons, resulting in the inability to send emails. .

The following is the solution:

First, remove the following two semicolons in php.ini

;extension=php_sockets.dll

;extension=php_openssl.dll

I had already removed it when I used PHPmailer before, here is just a reminder.

Then replace the fsockopen function

Replace the fsockopen function in the class.smtp.php file with the pfsockopen function:


 1 $this->smtp_conn = @fsockopen($host,    // the host of the server 
 2                                                    $port,    // the port to use 
 3                                  $errno,   // error number if any 
 4                                  $errstr,  // error message if any 
 5                                  $tval);   // give up after ? secs 
 6  
 7  
 8 //fsockopen改为: 
 9 $this->smtp_conn = @pfsockopen($host,    // the host of the server
 10                                  $port,    // the port to use
 11                                  $errno,   // error number if any
 12                                  $errstr,  // error message if any
 13                                  $tval);   // give up after ? secs

After setting this up, I can successfully send emails. If you also have this problem, you can try it by referring to the above example.


The above is the detailed content of Solution to SMTP Error: Could not connect to SMTP host when phpmailer sends email. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn