views:

66

answers:

4

Hi everyone,

I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.

I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.

Thanks, Amit

For clarification purposes, here is the mail-script.

<?php

$to = '[email protected]';
$subject = 'Contact from your website';

$message = 
'Below are details from the Contact Us Form ' . "\n\n" . 
'Name: ' . $_REQUEST['name'] . "\n\n" . 
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" . 
'E-mail address: ' . $_REQUEST['email'] . "\n\n" . 
'Comments: ' . $_REQUEST['comments'];

$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
            'Reply-To: ' . $email . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";       

//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];

if( $str != "10" || $strE == "")
{
    echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"&gt;&lt;!-- EDIT THIS -->

<?php   
} else {
    mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"&gt; <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
} 
?> 
+2  A: 

Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:

if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.

Posting full headers also can help legitimate messages pass spam tests.

   $headers = "Return-path: <[email protected]>\n";
                $headers .= "Reply-to: <[email protected]>"."\n";
                $headers .= "Content-Type: text/html; charset=windows-1252\n";
                $headers .= "Content-Transfer-Encoding: 7bit\n";
                $headers .= "From: <[email protected]>\n";
                $headers .= "X-Priority: 3\n";
                $headers .= "MIME-Version: 1.0\n";
                $headers .= "Organization: My Organization\r\n"; 
                $headers .= "\n\n";
DeaconDesperado
Could you take a look at my headers and tell me if they're okay?
Amit
+1  A: 

Write a really simple script, like

<?php
mail('[email protected]', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';

Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).

If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?

phihag
I see. Good idea, I'll give it a shot!
Amit
I tried it. It worked on the web-host server that had the script that works, and on the problematic web-host server it did not work. How can I solve it?
Amit
@Amit contact the support of the problematic webserver. It's probably blocked by default to prevent spamming or so.
phihag
+1  A: 

Whatever the solution, check your mail()'s output.

Most common solution

Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).

Alternative

The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).

Sam Bisbee
Do you think if the web-hosting company does not have SMTP set up, it would not work?
Amit
Absolutely! SMTP is what sends e-mail from one machine to another, so if they don't provide you with a way to make an SMTP connection, then you're not sending e-mail. Your script can connect to localhost, another SMTP gateway (IP/hostname) that they've set up, or your company's.
Sam Bisbee
Just so we're talking about the same thing. I thought SMTP requires authorization...When a PHP mail script uses the mail() function, it sends the email via SMTP? That means it does not go through authorization, how can it send it then?
Amit
Authorization is optional. Often web hosts will install an SMTP server on the localhost machine and bind it to only listen for incoming connection from 127.0.01. More than likely if they're using an alternative gateway (like smtp.example.com), they'll give you some login credentials or have it only listening for connections from internal machines.
Sam Bisbee
+1  A: 

If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)

I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)

Ankit Jain
This fixed it! thank you very much!
Amit