Log in

View Full Version : PHP contact form mailer problem...



lcot
10-03-2010, 01:46 PM
Hello, I am having a problem with my contact form php script.
When I use the form and submit it, it sends an email to the right place and the body and subject of the mail is correct but the values that I typed in such as name and email do not show up. In the email it is simply:

The website where I am implementing this mailer to test it is here:
http://www.livingfortodayband.co.uk/basement2/contact.html
(http://www.livingfortodayband.co.uk/basement2/contact.html)
______________________________________…

Name:
Email:
Artist:
Subject:
Message:

It is empty. My current PHP code is as follows:




<?php

/* Subject and Email Variables */

$emailSubject = 'Basement Studio Web Enquiry';
$webMaster = 'info@basement-studios.co.uk';

/* Gathering Data Variables */

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$artistField = $_POST['artist'];
$subjectField = $_POST['subject'];
$messageField = $_POST['message'];

$body = <<<EOD
<br><hr><br>
Name: $name<br>
Email: $email<br>
Artist: $artist<br>
Subject: $subject<br>
Message: $message<br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$seccess = mail($webMaster, $emailSubject, $body, $headers);

/* Results as HTML */

$theResults = <<<EOD
<html><body>Congrats!!</body></html>
EOD;
echo "$theResults";

?>


The HTML of the form I am using is here :


<form action="mailer.php" method="post" enctype="multipart/form-data" id="contactform">
<ol>
<li>
<label for="name">First Name <span class="required">*</span></label>
<input id="name" name="name" class="text">
</li>
<li>
<label for="email">Your email <span class="required">*</span></label>
<input id="email" name="email" class="text" />
</li>
<li>
<label for="artist">Band/Artist Name</label>
<input id="artist" name="artist" class="text">
</li>
<li>
<label for="subject">Subject</label>
<input id="subject" name="subject" class="text">
</li>
<li>
<label for="message">Message <span class="required">*</span></label>
<textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li>
<label for="valid">Seven + Four =</label>
<input name="valid" type="text" class="text" id="valid" onblur="MM_validateForm('element_5','','NinRange11:11');return document.MM_returnValue" size="5" maxlength="2" if="element_5" />

</li>
<li class="buttons">
<input name="imageField" id="imageField" src="images/send.jpg" class="send" type="image">
<div class="clr"></div>
</li>
</ol>
</form>

I will be so grateful if someone can help me in finding out what is wrong with my script.. also if anyone has any idea how to involve an IP address of the person who submitted the form into the body of the email that would be fantastic

Thanks in advance

fastsol1
10-03-2010, 02:43 PM
You assign the values from the POST to namefield and such but you use $name in the body. $name is not set to anything which is why it's blank, change your body variables to those of your post variables.

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$artistField = $_POST['artist'];
$subjectField = $_POST['subject'];
$messageField = $_POST['message'];

$body = <<<EOD
<br><hr><br>
Name: $nameField<br>
Email: $emailField<br>
Artist: $artistField<br>
Subject: $subjectField<br>
Message: $messageField<br>
EOD;

lcot
10-03-2010, 03:22 PM
Ahh that solved it mate. Just another question. Using the current script I get all the fields in the message and the subject.. but I have no "from" in the message title. I want to be able to see who the email is from without opening it instead of just a subject in my junk-email folder.

THanks

fastsol1
10-03-2010, 03:43 PM
Your headers for the mail should be more like this:


$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Reply-To: <the email you want>' . "\r\n";
$headers .= 'From: "What ever you want it to be like the $email" <you@yourdomain.com>' . "\r\n";

I changed some wording in the code so you know that goes in what so read that and change it accordingly. So obviously delete the headers you have now.

lcot
10-03-2010, 03:57 PM
Hey im a little confused. If i do this then I won't get the email from the person who sent it, just from an email I type into the script... :S

In the email header I get the email from "$email <you@yourdomain.com>".
I need to see the persons email who actually emailed me here..

Thanks

fastsol1
10-03-2010, 04:06 PM
You mean the person that submitted the form you want the email From: to show the email they posted right? I said you had to change the info to accomadate your needs. So in your instance $email should be $emailField and then put you domain in the <> area.

fastsol1
10-03-2010, 04:19 PM
I see what you are talking about, I just tried it on my site. I'll look into this.

fastsol1
10-03-2010, 04:39 PM
Ok so without some more research and talking with other forum members, you could do this
$emailSubject = "$emailField - Basement Studio Web Enquiry";and it works. After several attempts I am not sure how to do a dynamic From: in an email, but this will at least show you the email without opening the email to see it.

traq
10-03-2010, 05:05 PM
<?php

$headers .= "From: $nameField <$emailField>"."\r\n";

?>

However, you should really be validating your form inputs, especially before you start using them in your email headers.

fastsol1
10-03-2010, 05:17 PM
Thanks Traq, that worked perfect. lcot - just change the variables in the code Traq posted to suite what you want to display when you get the email.