简体   繁体   中英

PHP form sends success message, but mail() fails and email is never received. What's wrong with this PHP script?

I've been working with this for a little while and I cannot seem to get the PHP mail() function to work. I can't find the error in the script below. It seems odd, but the script to work with particular domains (@gmail.com - email is received), (@me.com, and others - email is not received). 适用于特定域(@ gmail.com-接收到电子邮件),(@ me.com和其他-未接收到电子邮件)。 This is the code - all it has to do is take the form and send the data to the email address provided. Any insight would be greatly appreciated - I'm not very familiar with PHP, I wouldn't call myself a programmer, and I won't pretend to be - I'm a designer so please be gentle. This was from a template I purchased... so I didn't write it... :S

<?php

//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$message = ($_GET['message']) ?$_GET['message'] : $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

//Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$message) $errors[count($errors)] = 'Please enter your message.'; 

//if the errors array is empty, send the mail
if (!$errors) {

// ====== Your mail here  ====== //
$to = 'Customers Name <customer@customerswebsite.com>'; 
//sender
$from = $name . ' <' . $email . '>';

//subject and the html message
$subject = 'Message from your website'; 
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<table>
    <tr><td>Name:</td><td>' . $name . '</td></tr>
    <tr><td>Email:</td><td>' . $email . '</td></tr>
    <tr><td>Message:</td><td>' . nl2br($message) . '</td></tr>
</table>
</body>
</html>';

//send the mail
$result = sendmail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
    if ($result) echo 'Thank you! We have received your message.';
    else echo 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that 
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
    echo $result;   
}

//if the errors array has values
} else {}


//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";

$result = mail($to,$subject,$message,$headers);

if ($result) return 1;
else return 0;
}

?>

While this is likely an IT error, there are some things that you can do in regard to SMTP an attempt to get the e-mail messages through your server using code.

Things to watch out for related to IT:

  1. Make sure that the ip address of the server resolves back to the domain name for the website you're sending the email address from. If it says something like "localhost" in the mail headers then the message is likely being kicked because it isn't coming from a legitimate MTA. Since you were able to send it to a gmail account, check out the headers there .
  2. Verify that the ip address and domain name aren't on RBL or real time block lists. Shared hosting can cause someone else on the same box to have your IP address blocked. Here is a link to an RBL checker .
  3. Some domains require the use of SPF or sender profile framework records.

In my experience the best way to do this is to research headers and determine which server or service you're trying to target and their policies. Often a router won't give you feedback when it's bouncing your e-mail message. Sometimes it's due to the contents of the message, sometimes it's due to the way the message is constructed (incorrect MIME types).

While most PHP mail() app e-mails will make it through many firewalls, routers, and antispam clients, you may need to craft your message using sockets and SMTP alone. Check out the settings in mail() before you get into writing your own. Once you go down this path it can be very time consuming if you're not familiar with sockets and headers. Another question relating to configuring login parameters to use a real email account. This way you can actually receive the bounce backs if they exist.

Also consider forcing your PHP script to log into a real MTA and use a username and password for sending. This question may be helpful. This way if someone doesn't like the e-mails coming off of your web server you're not going to have your IP blocklisted or blacklisted.

Here is another way to send email through an MTA using Pear .

It's really going to come down to hacking the actual connection for the server you're trying to target without ending up on a blacklist.

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