Workaround for Phpmailer error SMTP error:could not connect to SMTP host

Source: Internet
Author: User
Tags php file

Phpmailer is a great open source messaging class, and it's very simple to use, but for a virtual host, there are always restrictions. Just now I encountered an "SMTP error:could not connect to SMTP host" error using Phpmailer on a virtual host. Here are two ways to fix this:

This error indicates that the virtual host does not support the Fsockopen function that phpmailer the default call, finds the class.smtp.php file, searches for Fsockopen, and finds such a piece of code:

Connect to the SMTP server
$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? secs

Method 1: Replace the Fsockopen function with the Pfsockopen function

Because the Pfsockopen parameters are basically consistent with the fsockopen, you just need to replace @fsockopen with @pfsockopen.

Method 2: Use the Stream_socket_client function

General Fsockopen () is prohibited, Pfsockopen may also be banned, so here is another function stream_socket_client ().

The Stream_socket_client parameter differs from the Fsockopen, so the code is modified to:

$this->smtp_conn = stream_socket_client ("tcp://". $host ":". $port, $errno, $errstr, $tval);

Add:

Solution: Many of the Web site listed the solution is due to the problem of SMTP case, although the nature of the problem is not here, but it does need to change this place, as for why, look at the following operation.

In class.phpmailer.php, you will:


function Issmtp () {$this->mailer= ' SMTP ';}

Change into:

function Issmtp () {$this->mailer = ' SMTP ';}

Instead of using SMTP to send mail, this place uses a different way of sending mail, checking the class.phpmailer.php file for the following paragraph:

Switch ($this->mailer) {
Case ' SendMail ':
return $this->sendmailsend ($header, $body);
Case ' SMTP '://Because SMTP and SMTP are not equal, you are choosing the following mailsend send mail instead of using SMTP
return $this->smtpsend ($header, $body);
Default
return $this->mailsend ($header, $body);
}
(2) Linux host disabled fsockopen () function

There are many space service providers in China to disable the server's Fsockopen function for security reasons.

Workaround:

Use the Pfsockopen () function instead of fsockopen (), and if the Pfsockopen function is disabled, you can also replace the socket function, such as stream_socket_client ()

Change @fsockopen to @pfsockopen in class.smtp.php

Put


$this->smtp_conn = @fsockopen ($host,//The host of the server

Change into:

$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? secs
(3) Firewall security settings rules, if the above two schemes are not working together, it is estimated that the rules of the firewall problem, you can let the server administrator to remove all the firewall rules, and then test to see if this is the reason.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.