简体   繁体   中英

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. 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. The mail function always returns true but I don't get any mail. The mail.log shows a bunch of timeouts. 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?

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. You can use a forwarder with Sendmail and send it locally unencrypted, and then have Sendmail forward it on the Gmail with TLS.

You may want to consider using a third party solution like PHPMailer. It supports SSL/TLS for Gmail, check out this example:

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

You can download PHPMailer from:

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

And here's another tutorial for PHPMailer:

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).

It appears that your ISP and/or your network/host's firewall may be disallowing outbound SMTP connections. 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).

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).

So my advice: pay for a secured VPN if that is what you so desire to do.

  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). You will have to change your IP (or pay for a secured VPN).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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