SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Setting Up Email Contact Form

    Hi,

    I have added the following contact form to my site however when I use the code it records that an email has been sent but no email is received at the respective email address.

    Is there anything I need to set up email system to work other than just the code on the page?

    Code:
    <table width="400" border="0" cellpadding="0" cellspacing="1"  bgcolor="#EDEDED" border="1">
    <tr>
    <td><form name="form1" method="post" action="contactsend.php">
    <table width="100%" border="0" cellspacing="1" cellpadding="3">
    <tr>
    <td>Subject:</td>
    <td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
    </tr>
    <tr>
    <td>Message:</td>
    <td><textarea name="detail" cols="50" rows="10" id="detail"></textarea></td>
    </tr>
    <tr>
    <td>Name:</td>
    <td><input name="name" type="text" id="name" size="50"></td>
    </tr>
    <tr>
    <td>Email:</td>
    <td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
    </tr>
    </table>
    </form>
    </td>
    </tr>
    </table>
    
    <?php
    // Contact subject
    $subject ="$subject"; 
    // Details
    $message="$detail"; 
    
    // Mail of sender
    $mail_from="$customer_mail"; 
    // From 
    $header="from: $name <$mail_from>"; 
    
    // Enter your email address
    $to ='*****@****.com'; 
    
    $send_contact=mail($to,$subject,$message,$header);
    
    // Check, if message sent to your email 
    // display message "We've recived your information"
    if($send_contact){
    echo "Thank You for Contacting Us. We will endevour to reply as soon as possible.";
    }
    else {
    echo "ERROR";
    }
    ?>

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    There is nowhere in your PHP where you extract the values passed from the form in the $_POST[] array into the variables that the PHP script is using. None of the vaiables your PHP script is using are ever given vallues to use in the subsequent processing.

    Note also that you shouldn't just directly assign the values across - you should validate the content of $_POST['customer_email'] using the appropriate validation filter to ensure that it is a valid email address format before moving the value to $customer_email (and similarly for all the other fields).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    Is there a template I can use for contact forms. They are widely used and to be honest it would be a good learning exercise.

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,616
    Mentioned
    413 Post(s)
    Tagged
    7 Thread(s)

  5. #5
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Brilliant thanks,

    I used a template and copied it directly. I have added my email address but no email is received.

    I have replaced it with email@test.com. I sthere anything else I need to change or set up on my server for emails to work?


    Code:
    <?php
    
    $EmailFrom = "Contact Us";
    $EmailTo = "email@test.com";
    $Subject = "Contact Form";
    $Name = Trim(stripslashes($_POST['Name'])); 
    $Tel = Trim(stripslashes($_POST['Tel'])); 
    $Email = Trim(stripslashes($_POST['Email'])); 
    $Message = Trim(stripslashes($_POST['Message'])); 
    
    // validation
    $validationOK=true;
    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 .= "Tel: ";
    $Body .= $Tel;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= $Message;
    $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=contactthanks.php\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>

  6. #6
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,616
    Mentioned
    413 Post(s)
    Tagged
    7 Thread(s)
    Have you changed your HTML? If you have something like $_POST['Name'], the work "Name" must correspond to the name="" attribute in your form code. You had name="name", which won't match because of the capital/lower case. My advice is never to use capitals in code. It leads to lots of unnecessary errors.

    And you won't pick up things like name="customer_mail".

    Also, make sure $EmailFrom corresponds to an email address.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    730
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralph.m View Post
    Have you changed your HTML? If you have something like $_POST['Name'], the work "Name" must correspond to the name="" attribute in your form code. You had name="name", which won't match because of the capital/lower case. My advice is never to use capitals in code. It leads to lots of unnecessary errors.

    And you won't pick up things like name="customer_mail".

    Also, make sure $EmailFrom corresponds to an email address.
    Hi,

    I copied it like for like. I have gone through it all and changed it to lower case.

    Not sure what you mean by this "Also, make sure $EmailFrom corresponds to an email address. " Do I need to put an email address in there? I thought was just a title.

  8. #8
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,616
    Mentioned
    413 Post(s)
    Tagged
    7 Thread(s)
    Maybe just repost all the code you have—HTML and PHP. It doesn't look like you've got a complete setup there. But yes, the $EmailFrom is expecting an email address.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi i have a similar issue ( but different in as my php form throws the error up and no email delivered) can i post it here or should i start a new thread?

  10. #10
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    23,616
    Mentioned
    413 Post(s)
    Tagged
    7 Thread(s)
    Quote Originally Posted by James1987 View Post
    can i post it here or should i start a new thread?
    Probably best to start a new thread.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •