PDA

View Full Version : Form wont send e-mail ?


spazzer
12-16-2010, 11:13 AM
Hi,
Apologies if have duplicated Post, my form wont send to my e-mail, I have placed this code in my mailer.php file, is it correctly filled out ?

<?php
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
header("location:error.php");
die();
}
//these are the form results
$name = $_POST['name'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];
$formsub = $_POST['Submit'];

if($formsub){

//this creates the body of the mail message
$mailMessage = "The message is: Hi Nik. You have a message from Nix Web Design";
$mailMessage .= "Name:" .$name."\n\n";
$mailMessage .= "E-Mail:" .$email."\n\n";
$mailMessage .= "Enquiry:" .$enquiry."\n\n";
$header = "From: nik@nixwebdesign.co.uk\r\n";
$header .= "Reply-To: nik@nixwebdesign.co.uk\r\n";
$header .= "Return-Path: nik@nixwebdesign.co.uk\r\n";
mail('nik@nixwebdesign.co.uk', 'Website Enquiry', $mailMessage,$header);

header("location:thanks.php");
exit;

}
?>

Thanks Nick C

Corrosive
12-16-2010, 01:34 PM
Can you post your form code because that all look OK to me.

Corrosive
12-16-2010, 01:37 PM
Try changing the name of your button to 'Submit' or change this code;

$formsub = $_POST['Submit'];

to

$formsub = $_POST['Button1'];

And see if that solves it.

spazzer
12-16-2010, 02:12 PM
Hi Corrosive,
Thanks for help, I have 4 files in my Contact Folder

contact.php
mailer.php
thanks.php
error.phpIs this correct ?

contact.php,thanks.php and error.php code below:

<div id="main">
<h2>contact nix web design</h2>
<h3> contact me</h3>
<p><span class="intro">Please feel free to Contact Me by any of the ways listed below or simply fill out the contact form and I will be in touch with you as soon as I can !</span>
<p class="contact">email - <a href="http://www.google.com/recaptcha/api/challenge?k=6LcPWr0SAAAAAM7QN9rjepjhuPJ-TL4BSjw4t8db"></script (nik@nixwebdesign.co.uk)>
<noscript><iframe src="http://www.google.com/recaptcha/api/noscript?k=6LcPWr0SAAAAAM7QN9rjepjhuPJ-TL4BSjw4t8db" height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"></noscript></div>
<script type="text/javascript">
window.onload = function() {
Recaptcha.focus_response_field();
}
</script>


<table cellspacing="0" cellpadding="4" border="0" style="font-family: Helvetica, sans-serif; color: #000000; font-size: 12px;"><tr><td><div><input type="submit" name="Button1" value="Submit">
</div></td>
<td><b style="line-height: 18px;">The words above come from scanned books. <br> By typing them, you help to digitize old texts.</b></td></tr></table>
<!-- ReCaptcha HTML and Script Finishes Here -->

</form>

<br />
<!-- main ends -->
</div>

mailer.php

<?php
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
header("location:error.php");
die();
}
//these are the form results
$name = $_POST['name'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];
$formsub = $_POST['Submit'];
if($formsub){
//this creates the body of the mail message
$mailMessage = "The message is: Hi Nik. You have a message from Nix Web Design";
$mailMessage .= "Name:" .$name."\n\n";
$mailMessage .= "E-Mail:" .$email."\n\n";
$mailMessage .= "Enquiry:" .$enquiry."\n\n";
$header = "From: nik@nixwebdesign.co.uk\r\n";
$header .= "Reply-To: nik@nixwebdesign.co.uk\r\n";
$header .= "Return-Path: nik@nixwebdesign.co.uk\r\n";
mail('nik@nixwebdesign.co.uk', 'Website Enquiry', $mailMessage,$header);
header("location:thanks.php");
exit;
}
?>

spazzer
12-16-2010, 02:18 PM
Soory all code not showing heres an upload of it !

Corrosive
12-16-2010, 05:22 PM
Try changing the name of your button to 'Submit' or change this code;

$formsub = $_POST['Submit'];

to

$formsub = $_POST['Button1'];

And see if that solves it.

Did you try this?

spazzer
12-16-2010, 09:49 PM
Hi Corrosive,
Yes I did try it but didn't change anything, not sure why this is happening, may have to begin from scratch again I think !!!!

Thanks for Help as always !

Nick C :confused:

Ricky55
12-17-2010, 04:18 PM
Spazzer I have a form package that I could send you if you're still struggling. Very easy to set up and use.

Email me richard@qwerty-design.co.uk if you want it.

spazzer
12-18-2010, 01:51 AM
Thanks Ricky,
Have sent e-mail mate, I'm sure its something simple and once I get it right will learn from it as always !

Cheers for help all :wink:

Ricky55
12-21-2010, 09:59 AM
Was that form ok fo you mate?

jmichae3
12-26-2010, 09:04 AM
I would change the PHP code:

<?php
require_once('recaptchalib.php');
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if(!$resp->is_valid) {
header("location:error.php");
die();
}
//these are the form results
$name = $_POST['name'];
$email = $_POST['email'];
if (strlen($email) > 64) {
//the RFC specification for emails is that
//they should not be longer than 64 characters
die('hack attempt');
}
$enquiry = $_POST['enquiry'];
$formsub = $_POST['Submit'];
if(isset($_POST['email']) && isset($_POST['name'])) {
//mail function and RFC requires an email address and a name to send mail to.
//it also requires a return address.
//this creates the body of the mail message
$mailMessage = "The message is: Hi Nik. You have a message from Nix Web Design";
$mailMessage .= "Name:" .$name."\n\n";
$mailMessage .= "E-Mail:" .$email."\n\n";
$mailMessage .= "Enquiry:" .$enquiry."\n\n";
$header = "From: nik@nixwebdesign.co.uk\r\n";
$header .= "Reply-To: nik@nixwebdesign.co.uk\r\n";
$header .= "Return-Path: nik@nixwebdesign.co.uk\r\n";
mail('nik@nixwebdesign.co.uk', 'Website Enquiry', $mailMessage,$header);
header("location:thanks.php");
exit;
}
?>

mail function and RFC requires an email address and a name to send mail to.
it also requires a return address and a from address. the from address should ALWAYS be the real address you are sending the mail from, even if it is a null/dummy email account on the server (that's what I use, an account that email is always sent from, but nothing ever goes to - no maintenance).
the example used in the php manual is
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();


but I discovered that my yahoo mail account was spam filtering out my php emails. it seems someone had flagged anything form that server as bad ,or anything from PHP/whatever as bad, or just server-sent emails. spam filters I guess look for real email headers. one header that always seems to get through is Outlook Express email headers. but they still get flagged sometimes anyway, and I have to add the item to my contacts list to make sure they don't get lost.

so check your spam filter or spam box.

the function I use for sending emails is

function is_valid_email($email) {
if (strlen($email)>64 || stristr($email, "\n") || stristr($email, "\r")/* || stristr($email, "\\")*/) {
return false;
}
return is_valid_email_address($email); //still don't work.
}

function normalize_linebreaks($text) {
$text = str_replace("\r\n", "\n", $text); /* win -> un*x */
$text = str_replace("\r", "\n", $text); /* mac -> un*x */
return $text;
}
function send_mail($sendername, $senderemail, $fromname, $fromemail, $contactname, $contactemail, $subject, $message) {
if (is_valid_email($contactemail)){
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
//$headers .= "X-Mailer: php/" . phpversion() . "\n";
$headers .= "X-Mailer: Microsoft Outlook Express 6.00.2900.2180\n";
$headers .= "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180\n";
$headers .= "From: \"".$fromname."\" <".$fromemail.">\n";
$headers .= "Reply-To: \"".$sendername."\" <".$senderemail.">\n";
$headers .= "Return-Path: <".$senderemail.">\n";
$x=mail("\"".$contactname."\" <".$contactemail.">", $subject, normalize_linebreaks($message), $headers);
// echo "<p>";
// print_r(error_get_last());
// echo "</p>\n";
return $x;
} else {
echo "<div style=\"color:red;\">The email address was incorrect.</div>\n";
}
return false;
}



//-----------------------
if (!isset($_POST['name']) || preg_match('/[\r\n]/',$_POST['name'])) {
// here, we found a newline or carriage return
// corrupted data should be set to empty string
$_POST['name']='';
}
if (!isset($_POST['email']) || preg_match('/[\r\n]/',$_POST['email'])) {
// here, we found a newline or carriage return
// corrupted data should be set to empty string
$_POST['email']='';
}
$txt = "Name: ".$_POST['name']."\n";
$txt .= "Email: ".$_POST['email']."\n";
$txt .= "Text: ".stripslashes($_POST['text'])."\n";
$txt .= "================================\n\n\n\n\n";
$txt .= "Note: you can reply to this email to get the person.\n";
if ('' != $_POST['name'] &&
'' != $_POST['email'] &&
'/home/nowhere/public_html' == $_SERVER['DOCUMENT_ROOT'] &&
/*send_mail($sendername, $senderemail, $fromname, $fromemail, $contactname, $contactemail, $subject, $message)*/
send_mail($_POST['name'],$_POST['email'],
"nowhere.com site", "nobody@nowhere.com", /*create a null email account with this name*/
"Webservant", "webmaster@nowhere.com", /*create a email forwarding account with this name*/
"my subject", $txt)
) {
echo "<div style=\"color:red;\">The email was successfully sent.</div>\n";
} else {
echo "<div style=\"color:red;\">";
echo "There was a problem sending the email. Sorry &mdash; Please report this error message to the webservant.</div>\n";
}

jmichae3
12-26-2010, 09:09 AM
don't make the form dependent upon a submit button with no name. that's not going to come across with any array index that I know of. at least give the submit button a name and a value.
<input type="submit" name="submit" value="submit"> would even be something that might come through (although I don't trust buttons to come through in web forms).

jmichae3
12-26-2010, 09:11 AM
if you don't understand, you were making your send code dependent upon the submit button being present in $_POST. I don't think that will happen if there is no name and value attribute in the input element.

Ricky55
12-26-2010, 01:44 PM
And if you weren't confused before you certainly will be now.

chemist71186
01-01-2011, 08:25 PM
Can someone take a look at this code and tell me why they think it will not collect the form information and send it to me at my email. Thanks.


Here is the form code

<form action="contact-form-handler.php" method="post" name="contact_form">

<h1 align="center">Question Form</h1>

<label for="first_name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;First Name: </label>
<input align="middle" name="first_name" id="first_name" size="30" /><br /><br />

<label for="last_name">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name: </label>
<input align="middle" name="last_name" id="last_name" size="30" /><br /><br />

<label for="email">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email Address: </label>
<input align="middle" name="email" id="email" size="30" /><br /><br />

<label for="Subject">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject: </label>
<input type="radio" name="Math" />Math
<input type="radio" name="Chemistry" />Chemistry<br /><br />

<label for="message">Message</label><br />
<textarea name="message" id="message" rows="20" cols="45"></textarea><br /><br />

<input type="submit" value="Submit" />
</form>


Here is the PHP form handler code

<?php
$errors = '';
$myemail = "mhebb@fscj.edu";//<-----Put Your email address here.
if(empty($_POST['first_name']) ||
empty($_POST['last_name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: All fields are required";
}

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email'];
$message = $_POST['message'];


if (!eregi(
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $first_name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $firstname . $last_name \n Email: $email_address \n Message \n $message";

$headers = "From: $myemail";
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact Form Handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>

edbr
01-03-2011, 04:11 AM
what is happening? not sending or empty? if empty echo $email_body on your process page to help de bug

chemist71186
01-03-2011, 01:44 PM
No empty. It will send to my gmail account but puts it in spam folder
But will not send to my college email account or my yahoo account. I am running it on my own server titles localhost on my Mac just for testing purposes only. I plan to put on with GoDaddy. I wonder if it actually works fine, and for some reason becuase I am using localhost as my own server trough apache, that the emails r going straight to spam folder

Corrosive
01-03-2011, 01:53 PM
It is a bit of a known issue with the $mail() function in PHP. You've done the right things in providing the $headers information etc. but I guess your college and Yahoo are a bit more picky.

edbr
01-04-2011, 02:18 AM
it could be because how you are sending it is rejected because of no mx setting, you will have to try it live. many spammers send from there own machines but this is picked up by anti spam.

jmichae3
01-08-2011, 03:55 AM
I had to set the from address to be an account on the server, an account which I never use and which gets no email. a dummy account. this way, it's always there as long as the server's there. but it's not fooplproof. everything went into my yahoo mail spam box. I guess yahoo flagged everything from that server. gaah! so I had to add that email address to my contacts to prevent it from going into the spam folder. or you can mark it as "not spam", but the former works better. looks like edbr knows more than I do about sending emails from servers.

edbr
01-08-2011, 04:21 AM
iits a mystery to me most of the time but i also have my own hosting reseller so i have 'some experience thats true