SMTP ERROR: Failed to connect to server: (0) error when phpmailer sends mail under linux

In the process of doing the project, the customer put forward the need to send emails in the later stage. Since the customer has a demand, there is nothing to say, let's go.

After searching for general information on the Internet, the PHPMailer plugin seems to work well, so I clone it from github , and the download link is PHPMailer.

The official demo is as follows: Of course, the relevant configuration should be replaced with your own

//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = customer messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'smtp.qq.com';
//$mail->Host = 'smtp.163.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 465;
$mail->SMTPSecure = "ssl";
//$mail->SMTPAuth = false;
//$mail->SMTPSecure = false;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'xxxxxxx@qq.com';
//$mail->Username = '[email protected]';
//Password to use for SMTP authentication
//$mail->Password = 'lingshuan008';
$mail->Password = 'password';
//Set who the message is to be sent from
//$mail->setFrom('[email protected] ',' fromuser ');
$mail->setFrom('[email protected]', 'fromuser');
//Set an alternative reply-to address
//$mail->addReplyTo('[email protected]', 'First Last');
$mail->addReplyTo('xxxxxxx@qq.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
$mail->msgHTML('hello,body!');
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

 

First of all, under the local windows, the sending is successful, it is very happy, then put it on linux and try it, once it runs, it appears

SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Then check the information in major technical forums; first, check that openssl and sockets on the linux server have been enabled, the fsockopen function has not been disabled, and the smtp service of the mailbox has also been enabled, but it still does not work. As for a saying that replacing smtp with SMTP, it can be sent successfully. Later, I checked the source code and found that this is only sent through sendmail, not smtp. Then, ping smtp.qq.com, telnet smtp.qq.com 465 directly on linux is no problem, but still send the above error, and later check the official website information and find the following code

$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
    )
);

I guessed whether my ssl authentication failed. After adding this code, a magical thing happened, and the email was sent successfully.

So far, the problem has been solved successfully.

If you have any questions, you can leave a message.

Please indicate the source!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324573077&siteId=291194637