Home >Backend Development >PHP Tutorial >Solution to SMTP Error when sending emails using phpmailer

Solution to SMTP Error when sending emails using phpmailer

黄舟
黄舟Original
2017-08-09 10:30:372633browse

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


public function IsSMTP() {
    $this->Mailer = 'smtp';
  }

//改成
public function IsSMTP() {
    $this->Mailer = 'SMTP';
  }

and then:


 switch($this->Mailer) {
        case 'sendmail':
          return $this->SendmailSend($header, $body);
        case 'smtp':
          return $this->SmtpSend($header, $body);
        default:
          return $this->MailSend($header, $body);
      }


//改成
switch($this->Mailer) {
        case 'sendmail':
          return $this->SendmailSend($header, $body);
        case 'SMTP':
          return $this->SmtpSend($header, $body);
        default:
          return $this->MailSend($header, $body);
      }

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 again, 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:


$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


//fsockopen改为:
$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

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 when sending emails using phpmailer. 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