user3445112
user3445112

Reputation: 201

posting contact form to email using php not working

I have created a 'coming soon' landing page for my new website. Until the website design and development has been completed I just want to be able to collect interested parties email addresses - which should just submit to directly to my email. I have written the below code, however I am not receiving any emails to my address during testing:

HTML:

form method="post" name="myemailform" action="emailform.php">
<div id="subscribe">
    <input type="text" placeholder="enter your email address..." name="emailAddress" id="emailAddress">
    <input type="submit" value="Submit">
    <div class="clear"></div>
  </div>
</form>

PHP:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to hit submit";
}
$visitor_email = $_POST['emailAddress'];
$email_from = $_POST['emailAddress'];

// Validate FIRST
if(empty($visitor_email))
{
    echo "Email address is required";
    exit;
}

$email_from = '[email protected]';
$email_subject = "New Form";
$email_body = "You have received a new notifcation from $visitor_email.\n".

$to = "[email protected]";
$headers = "From: $email_from \r\n"

// SEND EMAIL
mail($to,$email_subject,$email_body,$headers);
?>

This is my first attempt at server-side scripting. Can you see anything incorrect in my code?

Upvotes: 0

Views: 105

Answers (4)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Your conditional statement

if(!isset($_POST['submit']))

is looking for a named submit button:

<input type="submit" value="Submit">

which should be

<input type="submit" value="Submit" name="submit">

Also you seem to be missing a < for form method="post" if that is your actual code.

You're also missing a semi-colon in $headers = "From: $email_from \r\n"

which should read as $headers = "From: $email_from \r\n";

Plus,

$email_body = "You have received a new notifcation from $visitor_email.\n".

remove the dot at the end and replace with a semi-colon

$email_body = "You have received a new notifcation from $visitor_email.\n";

and use error reporting

being

error_reporting(E_ALL);
ini_set('display_errors', 1);

placed just before your opening <?php tag, which will signal any errors if found in code.


Sidenote:

This name="myemailform" you don't need that.

Upvotes: 2

onkarsahas
onkarsahas

Reputation: 300

You need to have SMTP server setup on your machine to send mails.

Upvotes: -1

Jorge Caballero
Jorge Caballero

Reputation: 749

Do you have access to the php.ini? You need a working email system installed in your server or access to the php.ini to configure the SMTP parameters. What OS are you running?

Upvotes: 1

Fatmike
Fatmike

Reputation: 11

Are you running the test from your local machine or from the hosting server? The requirements of sending emails can be found here: http://php.net/manual/en/mail.requirements.php

Upvotes: 1

Related Questions