View Full Version : Php contact form sent to e-mail
shika
08-03-2011, 05:55 PM
Hi,
I have made a contact form for my web site and when
it is sent it should go to my e-mail. The form works, just not consistantly.
Can you take a look at it and tell me what needs to be changed?
I don't get how it can work sometimes and then not other times.
Thanks,
if (isset($_REQUEST['txteMail']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['txteMail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{ //send email
$to = 'someone@cox.net' . ',' ; // note the comma
$to .= 'someone@yahoo.com';
$subject =($_REQUEST['txtSubject']);
$message =($_REQUEST['txtName']) . "\r ";
$message .=($_REQUEST['txtLName']) . "\r\n";
$message .=($_REQUEST['txtMsg']) . "\r\n";
$message .= "Checked Boxes: " . $products_msg ."\n";
$headers =($_REQUEST['txteMail']);
//mail($to, $subject, $message, $headers);
mail($to, $subject, $message, $headers );
}
JShor
08-03-2011, 07:02 PM
It could be a problem with your SMTP server. Also, can you post your HTML form that sends the data to this script?
Maybe there is something there that is tripping up the script.
prasanthmj
08-04-2011, 05:06 AM
Add a 'From' header to $headers
Also see the link below:
Why is my PHP script not sending emails? (http://www.html-form-guide.com/email-form/php-script-not-sending-email.html)
djr33
08-04-2011, 05:12 AM
Make a simpler email test page, removing everything that is extra. Use one email address, not two, and don't use variables-- just add test content.
Then test that page to see if it also does not always work. If it does always work, then something is wrong with this current page. If it does not always work, then something is wrong with your server.
The only thing that looks at all strange to me is the two email addresses, but that is probably irrelevant because if it works sometimes, then that can't be wrong.
Have you checked your spam folder? Automated emails like that often get placed in spam by email filters.
shika
08-04-2011, 05:23 AM
This is the code at the beginning of the page : (and the rest of the form) I have added in a write to text file so that if the e-mail doesn't work I can still check if the form was filled out. I appreciate you looking at this.
[CODE]
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
ob_start();
}
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}[CODE]
[CODE]
//this code puts the checkbox values into a variable that prints out in an email or form
if (isset($_POST["products"]))
{
foreach($_POST['products'] as $value) {
$products_msg .= "$value, ";
}
// prepare email body text for the checkboxes
$Body .= "products: ";
$Body .= $products_msg;
}
if (isset($_REQUEST['txteMail']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['txteMail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{ //send email
$to = 'Someone@cox.net' . ',' ; // note the comma
$to .= 'someone@yahoo.com';
$subject =($_REQUEST['txtSubject']);
$message =($_REQUEST['txtName']) . "\r ";
$message .=($_REQUEST['txtLName']) . "\r\n";
$message .=($_REQUEST['txtMsg']) . "\r\n";
$message .= "Checked Boxes: " . $products_msg ."\n";
$headers =($_REQUEST['txteMail']);
//mail($to, $subject, $message, $headers);
mail($to, $subject, $message, $headers );
}
// print to text file for back up
//load variables
$filename = "myForm.txt";
$fp = fopen($filename,"a") or exit("unable to open file");
$counter = $_SESSION['count'];
$counter ++;
$fp = fopen($filename,"a") or exit("unable to open file");
$date = date("l, F j, Y") . " ";
$time = date("h:i A") . "\r\n";
fwrite($fp, "<br />" . $date);
fwrite($fp, $time);
fwrite($fp, "<p>Name: " . $_POST["txtName"]) . "\r\n";
fwrite($fp, " " . $_POST["txtLName"]) . "\r\n </p>";
fwrite($fp, "<p>Subject: " . $_POST["txtSubject"]) . "\r\n </p>";
fwrite($fp, "<p>E-Mail: " . $_POST["txteMail"]) . "\r\n </p>";
fwrite($fp, "<p>Inquery: " . $_POST["txtMsg"]) . "\r\n </p>";
fwrite($fp, "<p>Checked Boxes: " . $products_msg ) . "\r\n </p>";
fclose($fp);
$url = "http://dianesepanski.com/form.php";
header("Location: $url");
} [CODE]
<div id="mainContent" style="color:#666666;">
<form action="contact.php" method="post" >
<h2>Contact Form</h2>
<p>Please fill out the form below.
Questions, comments or to be added to my e-mail list.<br />
Updates, newsletter, products and more!</p>
<div id ="notifyMe" style="float:right;padding-right:28%;text-align:left;">
<h3 style="margin-left:2%;">Notify me when: </h3>
<p><input type="checkbox" name="products[]" value="NewProduct" /> New Product is listed for sale<br />
<input type="checkbox" name="products[]" value="ShowUpdate" />When Show Info is Updated<br />
<input type="checkbox" name="products[]" value="Newsletter" /> Add Me to Your Newsletter<br />
<input type="checkbox" name="products[]" value="Blog" />When Blogs Are Updated<br />
<input type="checkbox" name="products[]" value="ClassPost" />When Classes are Posted<br />
<input type="checkbox" name="products[]" value="All" /> All of the Above<br />
</p></div>
<div id="formInfo" style="padding-left:25%;text-align:left;"><p>
<label>
<input type="text" name="txtName" id="txtName" />
First Name </label>
</p>
<p>
<label>
<input type="text" name="txtLName" id="txtLName" />Last Name
</label>
</p>
<p>
<label>
<input type="text" name="txteMail" id="txteMail" />E-mail </label></p>
<p>
<label>
<input type="text" name="txtSubject" id="txtSubject" />Subject
</label>
</p>
<p> <textarea name="txtMsg" cols="50" rows="6"></textarea>
Message</p>
<p> <input name="Submit" type="submit" value="submit" /><input name="Reset" type="reset" value="Reset" /></p>
</div>
</form>
JShor
08-04-2011, 05:38 AM
As Daniel said, make a simpler email page and debug it from there.
Also, submit your form in different circumstances (like some fields checked, some fields entered, etc). That might affect what is causing the form to send (or not send at all).
shika
08-05-2011, 04:24 PM
Thanks for the advice.
I have tried adding the "From" to the headers. and It
sends after a few minutes.
I will start debugging now and see if it will send any quicker.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.