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.

php contact form not working

Featured Replies

I'm no php boff (complete the opposite! total noob!)

 

I have a contact form on one of my sites, that just wont work.

 

Can someone pls check the code to see if there are any errors anywhere.... thanks :)

 

<form method="post" action="contact.php"> 
                        <fieldset> 

                               <legend>How can we help you?</legend> 
                           <div id="formconts"> 
                               <p class="formp"><label>Email:</label> 
                               <input type="text" name="EmailFrom"/> </p> 
                               <p><label>Name:</label> 
                               <input type="text" name="Name"/></p>
                               <p><label>Company name:</label> 
                               <input type="text" name="Companyname"/></p>  
                               <p><label>Telephone:</label> 
                               <input type="text" name="Telephonenumber"/></p>                         
                               <p><label>Message:</label>                       
                               <textarea name="Yourmessage" cols="23" rows="5"></textarea></p> 
                           </div> 
                        </fieldset> 

                           <div class="buttons"> 
                               <input type="submit" name="submit" value="Send form"/> 
                               <input type="reset" name="reset" value="Reset" /> 
                           </div> <!--buttons end--> 

                       </form> 

 

<?php

// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "helenguttridge@live.com";
$Subject = "Electrod UK contact form completion";
$Name = Trim(stripslashes($_POST['Name'])); 
$Companyname = Trim(stripslashes($_POST['Companyname'])); 
$Telephonenumber = Trim(stripslashes($_POST['Telephonenumber'])); 
$Yourmessage = Trim(stripslashes($_POST['Yourmessage'])); 

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
 exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Companyname: ";
$Body .= $Companyname;
$Body .= "\n";
$Body .= "Telephonenumber: ";
$Body .= $Telephonenumber;
$Body .= "\n";
$Body .= "Yourmessage: ";
$Body .= $Yourmessage;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
 print "<meta http-equiv=\"refresh\" content=\"0;URL=index.htm\">";
}
else{
 print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

First, remove the <> from

"From: <$EmailFrom>"

 

ie.

"From: $EmailFrom"

 

 

Next. It is not a good idea to have the email from the person who filled in the form as the email is being sent from your site.

 

 

So use this.

 

$header = "MIME-Version: 1.0\r\n";
$header .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
$header .= "From: ".$FromAddress."\r\n";
$header .= "Reply-To: ".$email."\r\n";

 

mail($email, $Subject , $Body, $header,'-f'.$FromAddress

  • Author

Ok... thanks :)

 

Turns out the form wasn't the problem.... it just wouldn't send to my email address. It was fine with another address though... strange!

Ok... thanks :)

 

Turns out the form wasn't the problem.... it just wouldn't send to my email address. It was fine with another address though... strange!

 

The reason is that you were sending it 'from' an email address that was not related to the site you were on. Some email clients will not accept emails where the from address is not the same as the site from which it was sent. That's why some email clients accept it (the less fussy ones) and some reject it.

You may be able to get your hosting company to set up a SPF record for your website so that email clients don't associate your website with spam - whether or not they will depends on the hosting type you have really. If you're on some super cheap shared hosting then chances are they wont do it unless you've been with them a while

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.