Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP Contact Form not sending Email

    Hi,

    I have created a simple Contact Form (following a tutorial, I have no prior experience in PHP), and most of the functionality seems to be there. However, the information submitted by the form is not being sent to the email address I have added into the code.

    Could anybody take a look and see what could be wrong?

    The contact form is here: http://enhancedigital.co.uk/contact.php

    On clicking 'Submit' you will be taken to a thank you page (email_success.php), but the email isn't coming through.

    Any help would be appreciated,

    Thanks!

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,298
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by enhancedigital View Post
    Could anybody take a look and see what could be wrong?

    The contact form is here: http://enhancedigital.co.uk/contact.php
    You realise that you're in a php forum don't you? So posting a link to a html page (even if generated by php) isn't of any use to us to solve you php issue. All we can do is see html and not php.

    What you need to do is post the relevant PHP code HERE in this topic wrapped in [php] tags. For more info on how to use the tags, see the first link in my signature. Also note the php code box below that demonstrates the effect it has and why we ask you to use them.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ah ok my apologies, you may now understand how little I currently know about this language! Below is the PHP, saved in a file called "thankyou.php".

    PHP Code:
    <?
    $name 
    $_POST['fullname'];
    $email $_POST['email'];
    $subject $_POST['subject'];
    $website $_POST['website'];
    $message $_POST['message'];

    $email_message "
    Name: "
    .$name."
    Email: "
    .$email."
    Subject: "
    .$subject."
    Website: "
    .$website."
    Message: "
    .$message."

    "
    ;

    mail('enhancedigital@gmail.com' 'New Enquiry' $email_message);
    header('location: email_success.php');
    ?>

    This code is linked to the form on the page I linked to in my first post, with the form code looking like this:

    <form action="thankyou.php" method="post">
    <table width="424" id="formtable">
    <tr>
    <td width="219" style="text-align:center">Name:</td>
    <td width="193" height="50">
    <input type="text" name="fullname">
    </td>
    </tr>
    <tr>
    <td style="text-align:center">Email Address:</td>
    <td height="50"><input type="text" name="email"></td>
    </tr>
    <tr>
    <td style="text-align:center">Subject:</td>
    <td height="50"><input type="text" name="subject"></td>
    </tr>
    <tr>
    <td style="text-align:center">Your Current Website Address:<br /> (if available)</td>
    <td height="50"><input type="text" name="website"></td>
    </tr>
    <tr>
    <td height="100" style="text-align:center">Message:</td>
    <td><textarea name="message" cols="30" rows="5"></textarea></td>
    </tr>
    </table>
    <div align="center" style="margin-top:20px;">
    <input type="submit" value="Submit">
    </div>
    </form>

    If there is any other information I need to post, let me know.

    Many Thanks!

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Not sure if I will be helpful but..

    It should be <?php not <?

    A mail server should be running on your server, which is probably the case. But do we need to register/activate a mailbox in order to send an email?

    The mail() function is immediately followed by header(). Maybe there needs to be a delay between these two, in case the mail isn't sent before the reload. Perhaps comment out the header() to test.

    Adding if (mail(...)) { /* mail was sent */ }
    may be helpful - it returns true if sent (although not a reliable indicator of success).

    Try creating a small test page to send an email. If this works then it indicates there is a problem with the post-data.

    BTW Once you've got it working you should clean/sanitize the post data before embedding it in an email.
    Last edited by AndrewGSW; 08-07-2012 at 11:20 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,298
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by AndrewGSW View Post
    A mail server should be running on your server, which is probably the case.
    No it just needs sendmail installed and available. It doesn't actually need a server as such. On windows platforms with WAMP setups you can get a free program called fakesendmail. Any Outbound MTA (Mail Transfer Agent) is basically a SMTP Client that makes outbound connections only.

    Quote Originally Posted by AndrewGSW View Post
    But do we need to register/activate a mailbox in order to send an email?
    Only for receiving email.

    Quote Originally Posted by AndrewGSW View Post
    The mail() function is immediately followed by header(). Maybe there needs to be a delay between these two, in case the mail isn't sent before the reload. Perhaps comment out the header() to test.
    No need. PHP code runs sequentially line by line. It will make its call to mail() and return with either a true or false result indicating that the email was accepted and queued for sending. The boolean result from mail() does not however indicate a successful send. Regardless of this, once the mail() call has returned, a header redirect will not stop an outgoing email from being sent in a seperate process.

    Quote Originally Posted by AndrewGSW View Post
    Adding if (mail(...)) { /* mail was sent */ }
    may be helpful - it returns true if sent (although not a reliable indicator of success).
    It only returns true if the MTA (Mail Transfer Agent) accepted the email. It doesn't mean in any way, shape or form that the email was sent out by the MTA successfully.

    Quote Originally Posted by AndrewGSW View Post
    BTW Once you've got it working you should clean/sanitize the post data before embedding it in an email.
    It doesn't really matter actually. You only need to sanitise data for a database call that inserts or updates data. You should however, sanitize the email address field to be sure that it doesn't contain more than one email address otherwise your form may be hit by bots for annonymously attacking mailboxes with unwanted junk from your site. This leads to two things: Your sites IP being blacklisted as a spammer and the target email boxes being suspended if they go over their quota.

    @enhancedigital:
    You are missing the additional headers - see this link: mail()

    More specifically, you need to specify who the email is from in your mail call after the message parameter. This is a common error when learning to use the mail function.

    Here is a sample code (untested):
    PHP Code:
    $From 'admin@domain.com';
    $name $_POST['fullname'];
    $email $_POST['email']; // You need to check this contains just one email address
    $subject $_POST['subject'];
    $website $_POST['website'];
    $message $_POST['message'];

    $Headers "From: $From";

    $email_message = <<<STOP
    Name: $name
    Email: $email
    Subject: $subject
    Website: $website
    Message: $message

    STOP;

    if (
    mail('enhancedigital@gmail.com' 'New Enquiry' $email_message$Headers))
       {
       
    header('location: email_success.php'); 
       }
    else
       {
       
    //Error - Mail not accepted for sending
       

    Last edited by tangoforce; 08-08-2012 at 06:14 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • The Following 2 Users Say Thank You to tangoforce For This Useful Post:

    AndrewGSW (08-07-2012), enhancedigital (08-08-2012)

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    @tangoforce
    Thank you
    (I did mention that the mail() return-result could not be relied upon..)

    I missed the From as well, Doh!

    I use Test Mail Server Tool to test locally (I'm on Windows).
    Last edited by AndrewGSW; 08-07-2012 at 11:54 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It seems to be working now with the From header added, the emails are coming through as I want them to.

    Thanks for your help!

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,298
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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