Simple Subscription Form - E-Mails not received though no errors are found.

Hi Guys,
First time poster here looking for a bit of help. I don’t have much experience in php everything I learned at uni is long gone in my mind! I’ve got a simple subscription form on my website, where users can type there e-mail and press subscribe, this then e-mails me with their e-mail address so I can add them to my database. Today I tried adding 2 more fields, one for First Name and one for Last Name. No errors show up, and when you click subscribe it takes you to the correct page showing a success message, the problem is now I do not receive any e-mail from this script. Please take a look and see if you can spot where I am going wrong as I am now at a loss!

Here is the code from mailinglist1.php;
[php]Contact Us<?php
if(isset($_POST[‘submit’])) {

$to = "[email protected]"; 
$subject = "Promotion Mailing List";
$email = $_POST['email'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$body = "\nE-Mail: $email \nFirst Name: $firstname \nLast Name: $lastname";

echo "Thank you for your interest.\n Visit http://www.fakelink.com to download your free voucher.";
mail($to, $subject, $body);

} else {
echo “Error 404 : Unauthorised entry.”;
}
?>[/php]

Here is the html for my form;

[code]

	<div class="form">
	  <table width="100%" border="0" cellspacing="0" cellpadding="5">
	    <tr>
<td><label for="email">E-Mail</label></td>
<td><input name="email" id="input" /></td>
</tr>
First Name Last Name  
	<div class="form"></div>

	</form>[/code]

Very grateful for anyone who can show me the stupid mistake I must be missing!

Regards, Mitchell

P.S: I would also like to know how I can redirect to a specific html page on my site once the user presses subscribe, this is not so important but would be useful.

first of all the code is open to many security risks so you need to make it more secure an example i found may help you.
[php]

<?php function spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}

if (isset($_REQUEST[‘email’]))
{//if “email” is filled out, proceed

//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST[‘email’]);
if ($mailcheck==FALSE)
{
echo “Invalid input”;
}
else
{//send email
$email = $_REQUEST[‘email’] ;
$subject = $_REQUEST[‘subject’] ;
$message = $_REQUEST[‘message’] ;
mail("[email protected]", “Subject: $subject”,
$message, “From: $email” );
echo “Thank you for using our mail form”;
}
}
else
{//if “email” is not filled out, display the form
echo "
Email:

Subject:

Message:




"; } ?> [/php]

At first glance, the code looks fine to me. As the previous poster put, you should validate your fields before you use them (never trust user input) and also I would check if the $_POST values are set (using isset as is used with $_POST[‘submit’]) to ensure that the user has sent them. Can you post the code that worked?

Sponsor our Newsletter | Privacy Policy | Terms of Service