Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-07-2012, 07:26 PM   PM User | #1
enhancedigital
New Coder

 
Join Date: Aug 2012
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
enhancedigital is an unknown quantity at this point
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!
enhancedigital is offline   Reply With Quote
Old 08-07-2012, 07:52 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 08-07-2012, 08:59 PM   PM User | #3
enhancedigital
New Coder

 
Join Date: Aug 2012
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
enhancedigital is an unknown quantity at this point
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!
enhancedigital is offline   Reply With Quote
Old 08-07-2012, 11:18 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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.
__________________
"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

Last edited by AndrewGSW; 08-07-2012 at 11:20 PM..
AndrewGSW is offline   Reply With Quote
Old 08-07-2012, 11:40 PM   PM User | #5
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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
   

__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 08-08-2012 at 06:14 PM..
tangoforce is offline   Reply With Quote
The Following 2 Users Say Thank You to tangoforce For This Useful Post:
AndrewGSW (08-07-2012), enhancedigital (08-08-2012)
Old 08-07-2012, 11:51 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
@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).
__________________
"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

Last edited by AndrewGSW; 08-07-2012 at 11:54 PM..
AndrewGSW is offline   Reply With Quote
Old 08-08-2012, 10:47 AM   PM User | #7
enhancedigital
New Coder

 
Join Date: Aug 2012
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
enhancedigital is an unknown quantity at this point
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!
enhancedigital is offline   Reply With Quote
Old 08-08-2012, 06:41 PM   PM User | #8
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,667
Thanks: 46
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:13 AM.


Advertisement
Log in to turn off these ads.