简体   繁体   English

带有php的电子邮件:[SMTP错误:无法连接到SMTP主机]

[英]Email with php:[SMTP Error: Could not connect to SMTP host]

I have search many similar questions asked before but I still cannot find the solving method for this. 我已经搜索过很多类似的问题,但是仍然找不到解决方法。 Sorry for asking this question. 很抱歉问这个问题。

Below is my php coding using to send email: 下面是我用来发送电子邮件的php编码:

<?php
require("class.phpmailer.php"); // path to the PHPMailer class

$mail = new PHPMailer();  

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "testingtesting0330@gmail.com"; // SMTP username
$mail->Password = "xxxxxxxx"; // SMTP password 

$mail->From     = "testingtesting0330@gmail.com";
$mail->AddAddress("testingtesting0330@gmail.com");  

$mail->Subject  = "First PHPMailer Message";
$mail->Body     = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;  

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>

The class.phpmailer.php file is placed in the same file with the previous php document. class.phpmailer.php文件与之前的php文档放置在同一文件中。 I have also changed the content of class.phpmailer.php as follow: 我还更改了class.phpmailer.php的内容,如下所示:

  var $Host        = 'ssl://smtp.gmail.com';

  var $Port        = 465;

I have also change the content of php.ini: 我还更改了php.ini的内容:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = testingtesting0330@gmail.com

The openssl function is also enabled in php.ini: 在php.ini中也启用了openssl函数:

extension=php_openssl.dll

It also show openssl enabled in php info. 它还显示了在PHP信息中启用的openssl。

But after this, the error did show again. 但是在此之后,错误再次显示。 So, I would like to ask for the solution for this. 因此,我想为此寻求解决方案。

Also, if I need to connect to a certain SMTP server, only change the coding and php.ini is enough? 另外,如果我需要连接到某个SMTP服务器,只需更改编码,而php.ini就足够了吗?

Thank you guy very much. 非常感谢你们。 This is very important to me and hope this problem can be solved. 这对我来说非常重要,希望这个问题可以解决。

After you guy helping me, I have tried more method to solve the problem. 在您帮助我之后,我尝试了更多解决问题的方法。

I have change the SMTPSecure from "ssl" to "tls". 我已将SMTPSecure从“ ssl”更改为“ tls”。

$mail->SMTPSecure = 'tls'; $ mail-> SMTPSecure ='tls';

The error change from "SMTP -> ERROR: Password not accepted from server" to "SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known" 错误从“ SMTP->错误:服务器未接受密码”更改为“ SMTP->错误:无法连接到服务器:php_network_getaddresses:getaddrinfo失败:未知此类主机”

Further, if I changed the host from smtp.gmail.com to my IP address, 此外,如果我将主机从smtp.gmail.com更改为我的IP地址,

the error became "ERROR: Failed to connect to server: connection attempt failed because the connection object has for some time did not respond correctly, or connection establishment failed because the host connection can not respond" 该错误变为“错误:无法连接到服务器:连接尝试失败,因为连接对象一段时间未正确响应,或者连接建立失败,因为主机连接无法响应”

Many thanks to all of you. 非常感谢大家。

I think there is an issues with the server firewall. 我认为服务器防火墙存在问题。 Can you please try to stop server firewall for testing purpose and check this again. 您能否尝试停止服务器防火墙以进行测试,然后再次检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM