3

Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).

<form method="POST" name="contactform" action="contact-form-handler.php">
    <p>
        <label for='name'>Your Name:</label>
        <br>
        <input type="text" name="name">
    </p>
    <p>
        <label for='email'>Email Address:</label>
        <br>
        <input type="text" name="email">
        <br>
    </p>
    <p>
        <label for='message'>Message:</label>
        <br>
        <textarea name="message"></textarea>
    </p>
    <input type="submit" value="Submit">
    <br>
</form>

And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.

<?php 
$errors = '';
$myemail = 'EMAIL@gmail.com';
if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
    $to = '$myemail';
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n ".
        "Email: $email_address\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";
    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
}
?>

Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.

antyrat
  • 27,479
  • 9
  • 75
  • 76
Tyler
  • 81
  • 1
  • 2
  • 7
  • mail() is returning a bool. Maybe you like to check that. Is your mail server configuration correct? – frlan Jan 04 '14 at 23:27
  • you haven't echoed `$errors` so you never seen it. `if( empty($errors)) { }else echo $errors;` – Jérôme Teisseire Jan 04 '14 at 23:36
  • and your `preg_match` return always `$errors .= "\n Error: Invalid email address";` , so `empty($errors)` always false and your email is never send – Jérôme Teisseire Jan 04 '14 at 23:45
  • Thanks everyone who helped, it was a configuration issue with my mail server. The code all worked, and the error bit was in a separate bit that I didn't post. – Tyler Jan 05 '14 at 01:46

4 Answers4

11

*PHP to send form data to an email i have used this code as well as its work for me .you can try *

<?PHP
$email = $_POST["emailaddress"];
$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n

Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>

after that you can addded your futher code

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
1

Try add in top file: error_reporting(E_ALL);

and edit your code, see:

if(mail($to,$email_subject,$email_body,$headers)){
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} else {
    echo 'Error!';
}

Read this:

Protomen
  • 9,471
  • 9
  • 57
  • 124
0

you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work

Chris
  • 113
  • 4
0

The problem is with your From field in your $headers variable. you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.

The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.

I've already mentioned it in details here.

Community
  • 1
  • 1
The SuperKat
  • 234
  • 2
  • 10