2013-03-21 114 views
3

我对PHP相当陌生,最近我尝试使用邮件功能,但它不起作用。这里是我的代码:使用php发送邮件不起作用

<?php 

// Pick up the form data and assign it to variables 
$name = $_POST['name']; 
$email = $_POST['email']; 
$topic = $_POST['topic']; 
$comments = $_POST['comments']; 

// Build the email (replace the address in the $to section with your own) 

$to = '[email protected]'; 
$subject = "New message: $topic"; 
$message = "$name said: $comments"; 
$headers = "From: $email"; 

// Send the mail using PHPs mail() function 
if(mail($to, $subject, $message, $headers)) header("Location:../forms/success.html"); 
else header("Location:../forms/failure.html"); 
?> 

我将sendmail的路径添加到php.ini文件,但它仍然不起作用。邮件功能总是返回true,但我没有收到任何邮件。 mail.log显示了一堆超时。这里有一些输出:

postfix/qmgr[3523]: BB800B124A: from=<[email protected]>, size=382, nrcpt=1 (queue active) 
postfix/qmgr[3523]: CB34EB0E9F: from=<[email protected]>, size=405, nrcpt=1 (queue active) 
postfix/qmgr[3523]: D6C18B0D7B: from=<[email protected]>, size=394, nrcpt=1 (queue active) 
postfix/smtp[3527]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c01::1b]:25: No route to host 
postfix/smtp[3533]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c01::1b]:25: No route to host 
postfix/pickup[3522]: 8A9EEB1823: uid=70 from=<_www> 
postfix/cleanup[3524]: 8A9EEB1823: message-id=<[email protected]> 
postfix/qmgr[3523]: 8A9EEB1823: from=<[email protected]>, size=392, nrcpt=1 (queue active) 
postfix/smtp[3526]: connect to mx3.hotmail.com[65.55.37.72]:25: Operation timed out 
postfix/smtp[3531]: connect to gmail-smtp-in.l.google.com[74.125.141.27]:25: Operation timed out 
postfix/smtp[3531]: connect to gmail-smtp-in.l.google.com[2607:f8b0:400e:c01::1b]:25: No route to host 
postfix/smtp[3531]: connect to alt1.gmail-smtp-in.l.google.com[2607:f8b0:400e:c02::1b]:25: No route to host 
+1

验证出站邮件端口(端口25)是否在防火墙中未被阻止。 – 2013-03-21 19:39:12

+2

检查您正在尝试使用本地Web服务器的php.ini – Fiarr 2013-03-21 19:39:32

+0

中的smtp设置? – Nikitas 2013-03-21 19:46:18

回答

0

你可以发布你的php.ini smtp部分?

如果您使用的是GMail(它看起来像您......),您将需要使用SSL/TLS,我不知道PHP是否本机支持SMTP。您可以使用Sendmail的转发器并在未加密的情况下在本地发送它,然后让Sendmail通过TLS在Gmail上转发它。

您可能要考虑使用PHPMailer等第三方解决方案。它支持Gmail的SSL/TLS,看看这个例子:

http://phpmailer.worxware.com/index.php?pg=examplebgmail

您可以从下载的PHPMailer:

http://sourceforge.net/projects/phpmailer/

这里是为PHPMailer的另一个教程:

http://phpmailer.worxware.com/index.php?pg=tutorial#2

编辑:正如马克说,你的ISP可能会阻止outboun d端口25. Gmail还支持端口465上的SMTP(但同样需要TLS)。

+0

感谢您的帮助亚当... – Tishpr 2013-05-24 10:44:58

0

看起来您的ISP和/或您的网络/主机的防火墙可能不允许出站SMTP连接。没有理由你应该看到有关操作超时的错误没有路由到主机

+0

好吧 - 我会检查出来 – Tishpr 2013-03-21 22:44:49

0

这里有几个可能的原因:

  1. 你的ISP(Verizon公司,AT & T,Comcast公司,或学校等)阻止出站端口(防止垃圾邮件和网络攻击)。

这是最可能的情况。基本上,你不能将电子邮件作为家庭网络上的电子邮件“主机”发送。你可以通过gmail或其他SMTP服务器设置中继服务器,但最近ISP变得更聪明,同时也阻止了中继请求(是的,他们确实读取了你发送或接收的每个数据)。

所以,我的建议是:如果这是你想做的事情,那就付出一个安全的VPN。

  1. 您不在家庭网络中,但您中继服务器/路由器会阻止您的出站请求。您必须找到一种方法在防火墙中转发您的端口。根据路由器或中继服务器的操作系统/固件,可能有数百万种不同的方式来转发您的端口。或者你可以完全关闭防火墙。

  2. 它完全没有你的手。您的IP被标记为垃圾邮件(很可能是某人偷走了您的IP并将其用于网络攻击)。你将不得不改变你的IP(或支付一个安全的VPN)。