...

View Full Version : PHP Contact Form not sending Email



enhancedigital
08-07-2012, 07:26 PM
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!

tangoforce
08-07-2012, 07:52 PM
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.

enhancedigital
08-07-2012, 08:59 PM
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".



<?
$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!

AndrewGSW
08-07-2012, 11:18 PM
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.

tangoforce
08-07-2012, 11:40 PM
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.



But do we need to register/activate a mailbox in order to send an email?

Only for receiving 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.

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.


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.


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() (http://uk.php.net/manual/en/function.mail.php)

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):


$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
}

AndrewGSW
08-07-2012, 11:51 PM
@tangoforce
Thank you :thumbsup:
(I did mention that the mail() return-result could not be relied upon..)

I missed the From as well, Doh!

I use Test Mail (http://www.toolheap.com/test-mail-server-tool/) Server Tool to test locally (I'm on Windows).

enhancedigital
08-08-2012, 10:47 AM
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! :thumbsup:

tangoforce
08-08-2012, 06:41 PM
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum