Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Contact Form Nightmare!

Featured Replies

Hi all,

 

Now my fingers have finally defrosted i can write this post.

 

I've been tinkering with this contact form for ages now and it just won't work.

 

It appears to send i.e. no errors are flagged up in code but the email doesnt arrive in my mailbox.

 

I was wondering if it is an issue with the code or something not activated on the hosting?

 

The site casn be foundhere

 

As always your help is very much appreciated.

 

Thanks

OD

Who are you hosted with? Also, it could be that error reporting is disabled, in which case you wouldn't see the errors. Posting your code would also help :)

  • Author

Thanks for the reply.

 

Its hosted with NuBlue on one of their reseller packages. Just checked the error log and doesn't appear to show anything other than the lack of a favicon that ive forgotten to upload.

 

The HTML is as follows:

 

<form name="contactform" method="post" action="sendmail.php">
<ul>
<li><span>Name</span><input type="text" name="visitor" maxlength="50" size="30"/></li>
<li><span>Tel</span><input type="text" name="telephone" maxlength="30" size="30"/></li>
<li><span>Email</span><input type="text" name="visitormail" maxlength="80" size="30"/></li>
<li><span>Enquiry</span><textarea name="enquiry" maxlength="1000" cols="25" rows="6"></textarea></li>
</ul>
<input class="sendbutton" type="submit" value="Submit"/>
</form>	

 

 

The PHP is

 

 


<?php

$visitor = $_POST['visitor'];
$telephone = $_POST['telephone'];
$visitormail = $_POST['visitormail'];
$enquiry = $_POST['enquiry'];

$sendTo = "alex@jumpcommunication.co.uk";


?>


<!--- Content =-->
<?php
if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
include 'errorpage.php';
die;
}

if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone))
{
include 'errorpage.php';
die;
}
?>

<h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4>
<!--- /Content =-->

<?php

$todayis = date("l, F j, Y, g:i a") ;

$subject = "Web Enquiry From Mobile Care";

$enquiry = stripcslashes($enquiry);

$message = " $todayis [GMT] \n
From: $visitor ($visitormail)\n
Telephone: $telephone \n
Enquiry: $enquiry \n
";

$from = "From: $visitormail\r\n";

mail($sendTo, $subject, $message, $from);
?>

Thanks again

 

OD

Hi odmassive,

 

This won't solve the problem you've got, but it will help you identify the problems you will definitely have with spam using the validation you've currently got in place.

 

Basic Validation

 

At present you don't check for minimum/maximum string lengths on any of the fields. I could successfully submit your form with a . in each input (except email which I go into more detail about below). You'd at least want to check that name/telephone were of a reasonable length and contained valid characters.

 

Telephone Validation

 

You could use something similar to this to ensure the phone number submitted was numeric (after stripping out +, -, (, ), and white space which I'd consider valid in the case of a telephone number).

 

if(is_numeric(str_replace(array('+', '-', '(', ')', ' '), '', $telephone))){ 
echo 'This is a telephone number!';
}

Email Validation

 

At the moment your email validation just checks for an @ and a . - This means I could successfully send a message if $visitormail == '@.'. A combination of filter_var() and checkdnsrr() would be a much more effective validation method. Failing that you could use Michael Rishton's email validation regex, the same expression used in PHP's logical filters C file.

 

Error Reporting

 

While in development, make sure error reporting and display errors are on... Always...

 

// Show ALL errors
ini_set('display_errors', 1);
error_reporting(-1);

So...

 

To start with I'd implement some proper validation as I've outlined above, then make sure error_reporting is turned on and go from there.

using mail() is unreliable. It may well be sending but your email client might be flagging it as spam, especially as you're sending it from the users email address, which your local sendmail server is not authoritative for. Try sending it from noreply@mobilecareltd.co.uk and set the Reply-To header from the enquirer.

 

Also try sending a test to a hotmail/live address, they will accept them but put them in the junk folder.

 

While in development, make sure error reporting and display errors are on... Always...

 

// Show ALL errors
ini_set('display_errors', 0);
error_reporting(-1);

 

That would actually turn the errors and error reporting off. They should be

// Show ALL errors
ini_set('display_errors', 1);
error_reporting(E_ALL);

 

Don't seem to be able to pick up what's wrong with the form though, perhaps the error reporting being on will give more information

That would actually turn the errors and error reporting off. They should be

// Show ALL errors
ini_set('display_errors', 1);
error_reporting(E_ALL);

 

Don't seem to be able to pick up what's wrong with the form though, perhaps the error reporting being on will give more information

 

My bad, corrected the ini_set after I posted it. The error_reporting is correct, -1 will report all errors, forever :)

  • Author

Thanks for all your feedback.

 

I've made some of the changes as you've suggested, the code is now as follows

 

<?php

// Show ALL errors
ini_set('display_errors', 1);
error_reporting(E_ALL);

$visitor = $_POST['visitor'];
$telephone = $_POST['telephone'];
$visitormail = $_POST['visitormail'];
$enquiry = $_POST['enquiry'];

$sendTo = "alex@jumpcommunication.co.uk";


?>


<!--- Content =-->
<?php



if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
include 'errorpage.php';
die;
}

if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone))
{
include 'errorpage.php';
die;
}
?>

<h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4>
<!--- /Content =-->

<?php

$todayis = date("l, F j, Y, g:i a") ;

$subject = "Web Enquiry From Mobile Care";

$enquiry = stripcslashes($enquiry);

$message = " $todayis [GMT] \n
From: $visitor ($visitormail)\n
Telephone: $telephone \n
Enquiry: $enquiry \n
";

$from = "noreply@mobilecareltd.co.uk";

mail($sendTo, $subject, $message, $from);


?>

 

 

I changed the sendto email to an aol address and it worked fine but as soon as I changed it to the original email it fails again.

 

Ive been reading about headers...do they need to be added?

 

Also is there a better way of sending other than mail()?

 

I'm new to php so apologies for the stupidity.

 

Thanks again

OD

Thanks for all your feedback.

 

I've made some of the changes as you've suggested, the code is now as follows

 

<?php

// Show ALL errors
ini_set('display_errors', 1);
error_reporting(E_ALL);

$visitor = $_POST['visitor'];
$telephone = $_POST['telephone'];
$visitormail = $_POST['visitormail'];
$enquiry = $_POST['enquiry'];

$sendTo = "alex@jumpcommunication.co.uk";


?>


<!--- Content =-->
<?php



if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
include 'errorpage.php';
die;
}

if(empty($visitor) || empty($visitormail) || empty($enquiry) || empty($telephone))
{
include 'errorpage.php';
die;
}
?>

<h4 id="mail">Thanks for contacting us <?php echo $visitor ?></h4><h4 id="mail">One of the team will be in touch soon</h4>
<!--- /Content =-->

<?php

$todayis = date("l, F j, Y, g:i a") ;

$subject = "Web Enquiry From Mobile Care";

$enquiry = stripcslashes($enquiry);

$message = " $todayis [GMT] \n
From: $visitor ($visitormail)\n
Telephone: $telephone \n
Enquiry: $enquiry \n
";

$from = "noreply@mobilecareltd.co.uk";

mail($sendTo, $subject, $message, $from);


?>

 

 

I changed the sendto email to an aol address and it worked fine but as soon as I changed it to the original email it fails again.

 

Ive been reading about headers...do they need to be added?

 

Also is there a better way of sending other than mail()?

 

I'm new to php so apologies for the stupidity.

 

Thanks again

OD

 

 

You can try my contact form script and see if it sends with that email address Rizo Contact

Try using standard RFC 2822 headers.

 


<?php

$todayis = date("l, F j, Y, g:i a") ;

$subject = "Web Enquiry From Mobile Care";

$enquiry = stripcslashes($enquiry);

$message = " $todayis [GMT] \n
From: $visitor ($visitormail)\n
Telephone: $telephone \n
Enquiry: $enquiry \n
";

$message = wordwrap($message, 70);

$from = 'From: noreply@mobilecareltd.co.uk' . "\r\n" .
   'Reply-To: '.$visitormail;

mail($sendTo, $subject, $message, $from);


?>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.