views:

22

answers:

2

Hi,

I've had something strange happen lately with php script I use to send emails from an online contact form and just wondered if any one could shed a little light on the issue.

I've had a php script which I use on multiple websites and it has always worked fine, but for some strange reason, I tried using in on one site and it just wasn't working.

I tried fiddleing with it and eventually realised that it was something to do with the following section of code:

this is the original section of code that usually works fine, but wasn't working for some reason:

$to = 'My Name <[email protected]>';

I then removed the name bit, so that the code looked like this:

$to = '[email protected]';

and now it sends the email through ok.

As I say, the top code usually works fine, so any ideas why this time I had to alter the code to get it to work?

Any possible explanations would be great :o)

Here's the full code:

<?php

require("is_email.php"); // email validation function

//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'];  
$telephone = ($_GET['telephone']) ?$_GET['telephone'] : $_POST['telephone'];
$address = ($_GET['address']) ?$_GET['address'] : $_POST['address'];
$enquiry = ($_GET['enquiry']) ?$_GET['enquiry'] : $_POST['enquiry'];
$calculation = ($_GET['calculation']) ?$_GET['calculation'] : $_POST['calculation']; 

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

//Server side validation for POST data
if (!$name) $errors[count($errors)] = 'Please click back and enter your name.';  
if (!$email) $errors[count($errors)] = 'Please click back and enter your email.'; 
else if (!is_email($email)) $errors[count($errors)] = 'Please click back as you may have entered an invalid email address.';
if (!$telephone) $errors[count($errors)] = 'Please click back and enter your telephone number.';
if (!$address) $errors[count($errors)] = 'Please click back and enter your address.';
if (!$enquiry) $errors[count($errors)] = 'Please click back and enter your enquiry.';
if ($calculation != '14') $errors[count($errors)] = 'Please click back and check you have correctly answered the simple calculation (in number format).';

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

    //recipient - change this to your name and email  
    $to = '[email protected]';     
    //sender  
    $from = $name . ' <' . $email . '>';

    //subject and the html message  
    $subject = 'Website Enquiry: ' . $name;   
    $email_body = '  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;  
    <head></head>  
    <body>  
    <table cellpadding="5" style="color:#757575;">  
        <tr><td style="color:#3b5998;">Name: </td><td>' . $name . '</td></tr>  
        <tr><td style="color:#3b5998;">Email: </td><td>' . $email . '</td></tr>  
        <tr><td style="color:#3b5998;">Telephone: </td><td>' . $telephone . '</td></tr>
        <tr valign="top"><td style="color:#3b5998;">Address: </td><td>' . nl2br($address) . '</td></tr>
        <tr valign="top"><td style="color:#3b5998;">Enquiry: </td><td>' . nl2br($enquiry) . '</td></tr>  
    </table>  
    </body>  
    </html>';

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

    //if POST was used, display the message straight away  
    if ($_POST) {  
        if ($result) echo 'Thank you! We have received your message.<br /><br /><a href="../enquiry_form.html">OK</a>';  
        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 {

    //display the errors message  
    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br />';    
    exit;  
}

//Simple mail function with HTML header  
function sendmail($to, $subject, $email_body, $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,$email_body,$headers);  

    if ($result) return 1;  
    else return 0;  
}  
?>  
A: 

Hosts can vary on their requirements for sendmail and depending on what sendmail software they are using. You may take this up with your hosting company and see if there are any caveats to making it work or if they know a better format.

Brad F Jacobs
Ok thanks, is there an advantage in having the name and the email address following the $to = ? it's just I can't think just now why it is an advantage. Thanks again for any input :o)
ade123
The only reason I can think of is just display preferences when the client opens an email. It would show from `My Name` with the top line, with the bottom it would show from the `[email protected]`
Brad F Jacobs
Thanks for your info premiso :o)
ade123
A: 

Use this:

$mailFrom = '"My Name" <[email protected]>';

the FROM label is double quoted and a space and <email>

Stewie
Thanks Stewie, but that didn't seem to work
ade123
How are you mailing ? using the mail() function OR opening a SMTP connection via script ?
Stewie
Hi Stewie, I've added the full code under my original post. I'm not that well up on php really.
ade123