[英]“Failed to connect to mailserver” when using SMTP
I'm trying to send an email with the following function as a test function: 我正在尝试发送具有以下功能的电子邮件作为测试功能:
function SendEmail($to, $subject, $body, $headers)
{
ini_set("SMTP", "smtp.myexistingdomain.com");
mail($to, $subject, $body, $headers);
}
and I get this error: 我得到这个错误:
Warning: mail() [function.mail]: Failed to connect to mailserver at "smtp.myexistingdomain.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
警告:mail()[function.mail]:无法通过“ smtp.myexistingdomain.com”端口25连接到邮件服务器,无法在php.ini中验证“ SMTP”和“ smtp_port”设置或使用ini_set()
PHP doesn't implement SMTP protocol. PHP不实现SMTP协议。 PHP's mail() - is wrapping around sendmail binary.
PHP的mail()-环绕sendmail二进制文件。 Works fine on unix/linux.
在unix / linux上正常工作。 But On Windows (from PHP.net manual):
但是在Windows上(来自PHP.net手册):
The Windows implementation of mail() differs in many ways from the Unix implementation.
mail()的Windows实现与Unix实现在很多方面有所不同。 First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
首先,它不使用本地二进制文件编写消息,而仅在直接套接字上运行,这意味着需要在网络套接字(可以在本地主机或远程计算机上)上侦听MTA。
So - the moral of the story - you need to install mail server. 所以-故事的寓意-您需要安装邮件服务器。
If you prefer to send emails without installing you own mailserver (which can be a pain in the *ss) - you must use a library that actually implements SMTP protocol and thus allows you to connect to someone else SMTP server. 如果您希望不安装自己的邮件服务器而发送电子邮件(在* ss中可能会很麻烦)-您必须使用一个实际上实现SMTP协议的库,从而使您可以连接到其他SMTP服务器。 These are most popular nowadays:
这些是当今最受欢迎的:
Using these libraries one can construct text or html messages with ease. 使用这些库,可以轻松构造文本或html消息。 Adding files also becomes an easy thing to do.
添加文件也变得很容易。
/*******************************************************************
Example of sending email using gmail.com SMTP server and phpmailer
*******************************************************************/
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
are you sure this domain works and an smtp server is active on port 25? 您确定该域有效并且端口25上的smtp服务器处于活动状态吗? secure smtp traffice uses a different port, google can tell u which in a sec :)
安全的smtp traffice使用其他端口,Google可以在几秒钟内告诉您哪个:)
you can try this if telnet is installed on your pc: 如果您的PC上安装了telnet,则可以尝试以下操作:
open a command prompt, type: telnet smtp.myexistingdomain.com 25 when it connects you can be sure the domain works and has a listening smtp server. 打开命令提示符,键入:telnet smtp.myexistingdomain.com 25当它连接时,您可以确定域可以正常工作并且具有侦听的smtp服务器。 if it fails, the smtp server is probably unreachable or uses a different port.
如果失败,则表明smtp服务器可能无法访问或使用其他端口。
Another thing you can check is if the right credentials are set in the php.ini file, most smtp servers require authentication. 您可以检查的另一件事是,如果在php.ini文件中设置了正确的凭据,则大多数smtp服务器都需要身份验证。
The SMTP setting is only available on Windows. SMTP设置仅在Windows上可用。 Are you able to connect directly to that host/port via telnet?
您是否可以通过telnet直接连接到该主机/端口? If something external to PHP, but running on the host, is unable to connect as well, then either that's not the right hostname, it's not listening on port 25, and/or there's a firewall issue.
如果PHP外部但在主机上运行的某个东西也无法连接,则可能是主机名不正确,未在端口25上侦听和/或存在防火墙问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.