简体   繁体   English

用PHP发送邮件不起作用

[英]Sending mail with php isn't working

I'm fairly new to php and I've recently tried using the mail function but it isn't working. 我是相当新的PHP,我最近尝试使用邮件功能,但它无法正常工作。 Here's my code: 这是我的代码:

<?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 = 'tishsny@gmail.com';
$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" );
?>

I added the path of sendmail to the php.ini file but it's still not working. 我将sendmail的路径添加到php.ini文件中,但它仍然无法正常工作。 The mail function always returns true but I don't get any mail. 邮件功能总是返回true但我没有收到任何邮件。 The mail.log shows a bunch of timeouts. mail.log显示了一堆超时。 Here's some of the output: 这是一些输出:

postfix/qmgr[3523]: BB800B124A: from=<_www@Leticias-MacBook-Pro.local>, size=382, nrcpt=1 (queue active)
postfix/qmgr[3523]: CB34EB0E9F: from=<_www@Leticias-MacBook-Pro.local>, size=405, nrcpt=1 (queue active)
postfix/qmgr[3523]: D6C18B0D7B: from=<_www@Leticias-MacBook-Pro.local>, 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=<20130321193156.8A9EEB1823@Leticias-MacBook-Pro.local>
postfix/qmgr[3523]: 8A9EEB1823: from=<_www@Leticias-MacBook-Pro.local>, 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

Can you post your php.ini smtp section? 你可以发布你的php.ini smtp部分吗?

If you're using GMail (which it looks like you are...) you will need to use SSL/TLS which I don't know if PHP supports natively with SMTP. 如果您正在使用GMail(它看起来像......),您将需要使用SSL / TLS,我不知道PHP是否支持本机SMTP。 You can use a forwarder with Sendmail and send it locally unencrypted, and then have Sendmail forward it on the Gmail with TLS. 您可以将转发器与Sendmail一起使用并以未加密的方式在本地发送,然后让Sendmail通过TLS在Gmail上转发它。

You may want to consider using a third party solution like PHPMailer. 您可能需要考虑使用PHPMailer等第三方解决方案。 It supports SSL/TLS for Gmail, check out this example: 它支持Gmail的SSL / TLS,请查看此示例:

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

You can download PHPMailer from: 您可以从以下位置下载PHPMailer:

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

And here's another tutorial for PHPMailer: 这是PHPMailer的另一个教程:

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

EDIT: As Mark said, your ISP might be blocking outbound port 25. Gmail also supports SMTP on port 465 (but again, you will need TLS). 编辑:正如马克所说,您的ISP可能阻止出站端口25. Gmail还支持端口465上的SMTP(但同样需要TLS)。

It appears that your ISP and/or your network/host's firewall may be disallowing outbound SMTP connections. 您的ISP和/或您的网络/主机的防火墙似乎可能不允许出站SMTP连接。 There's no reason you should be seeing those errors about Operation Timed Out and No route to host . 您没有理由看到有关操作超时无主机路由的错误。

Here are a few possible reasons: 以下是一些可能的原因:

  1. Your ISP (Verizon, AT&T, Comcast, or your school etc) blocked your outbound ports (to prevent spam and cyber attacks). 您的ISP(Verizon,AT&T,Comcast或您的学校等)阻止了您的出站端口(以防止垃圾邮件和网络攻击)。

This is the most likely case. 这是最可能的情况。 Basically you cannot send email as an email "host" at a home network. 基本上,您不能在家庭网络上将电子邮件作为电子邮件“主机”发送。 You can set up relay server via gmail or other SMTP server in the past, but lately the ISP got smarter and block the relay request as well (yes they do read every data you sent or receive). 您可以在过去通过gmail或其他SMTP服务器设置中继服务器,但最近ISP变得更智能并阻止了中继请求(是的,它们会读取您发送或接收的每个数据)。

So my advice: pay for a secured VPN if that is what you so desire to do. 所以我的建议是:支付安全的VPN,如果这是你想要做的。

  1. You are not at a home network, but you relay server/router blocks your outbound request. 您不在家庭网络,但中继服务器/路由器会阻止您的出站请求。 You must find a way to forward your port in the firewall. 您必须找到一种在防火墙中转发端口的方法。 Depending on the router or the relay server's operating system/firmware, there could be millions of different way to forward your port. 根据路由器或中继服务器的操作系统/固件,可能有数百万种不同的转发端口的方式。 Or you can shut the firewall off completely. 或者您可以完全关闭防火墙。

  2. It's completely out of your hand. 这完全不在你手中。 Your IP is being flagged as a spambot (most likely someone stole your IP and use it for cyber attack). 您的IP被标记为spambot(很可能有人窃取了您的IP并将其用于网络攻击)。 You will have to change your IP (or pay for a secured VPN). 您将不得不更改您的IP(或支付安全的VPN)。

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

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