contact form doesnt send email

thorjack1990

BANNED
Joined
Feb 15, 2013
Messages
71
Reaction score
9
Does any1 know what ive done wrong with this code? when i click send on the form it will redirect me to thank_you page and says successfully sent.,but i did'nt get anything on my email.,ive also checked on spam folder nothing there.
heres my code:

<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "[email protected]";


/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$contacts_page = "contacts.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";


/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$firstname = $_REQUEST['firstname'] ;
$email = $_REQUEST['email'] ;
$website = $_REQUEST['website'] ;
$comments = $_REQUEST['comments'] ;


/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}


// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email'])) {
header( "Location: $contacts_page" );
}


// If the form fields are empty, redirect to the error page.
elseif (empty($firstname) ||empty($email) || empty($comments)) {
header( "Location: $error_page" );
}


// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}


// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email", "Feedback Form Results",
$firstname, $email, $website ,$comments, "From: $email" );
header( "Location: $thankyou_page" );
}
?>
 
Hum ... Obviously you did not write those lines of code ... => You try to avoid injections (in an email?!) but you do not know how to use mail function

SORRY I WILL NOT ANSWER TO THIS THREAD UNTIL YOU ANSWER ALL THE PREVIOUS THREADS YOU LAUNCHED WITHOUT ANSWERING.
 
Your code seems to be wrong created, you cannot use all strings into mail() function such as firstname,lastname, website etc. - these strings have to be included in mail content or headers. Here's the updated code:

PHP:
// If we passed all previous tests, send the email then redirect to the thank you page.
else {

// Email content
$content = 'Website: ' . $website . '\r\n';
$content .= $comments . '\r\n';

// Additional headers
$headers = 'To: '.$firstname.' <'.$email.'>' . "\r\n";
$headers .= 'From: Webmaster <'.$webmaster_email.'>' . "\r\n";
mail($email, "Feedback Form Results", $comments, $headers);

header( "Location: $thankyou_page" );
}
 
Hum ... Obviously you did not write those lines of code ... => You try to avoid injections (in an email?!) but you do not know how to use mail function

SORRY I WILL NOT ANSWER TO THIS THREAD UNTIL YOU ANSWER ALL THE PREVIOUS THREADS YOU LAUNCHED WITHOUT ANSWERING.

ive thank you for all your replies from my previous post.
 
Your code seems to be wrong created, you cannot use all strings into mail() function such as firstname,lastname, website etc. - these strings have to be included in mail content or headers. Here's the updated code:

PHP:
// If we passed all previous tests, send the email then redirect to the thank you page.
else {

// Email content
$content = 'Website: ' . $website . '\r\n';
$content .= $comments . '\r\n';

// Additional headers
$headers = 'To: '.$firstname.' <'.$email.'>' . "\r\n";
$headers .= 'From: Webmaster <'.$webmaster_email.'>' . "\r\n";
mail($email, "Feedback Form Results", $comments, $headers);

header( "Location: $thankyou_page" );
}

ive inserted the code you give me.,but nothing happens.,thank you for helping
 
Accepted :)


Your mail function was bad, here is a correct version (also accepted on gmail, because mail function send by default mails that are rejected by gmail)
PHP:
srand((double)microtime()*1000000); 
$boundary = md5(uniqid(rand())); 
$texte .= "<center>Feedback Form Result</center>"; 


$headers = 'From: '.$firstname.' <'.$email.'>' . "\r\n" .
 'Reply-To: '.$email.'' . "\r\n";
$headers .="MIME-Version: 1.0\n"; 
$headers .="Content-Type: multipart/alternative;boundary=$boundary\n"; 




$message = "\nThis is a multi-part message in MIME format."; 
$message .="\n--$boundary\nContent-Type: text/html;charset=\"iso-8859-1\"\n\n"; 
$texte="Firstname: $firstname <br/>
Email: $email <br/>
Website: $website <br/>
Comment: $comments <br/>";
$message .= $texte;
$message .="\n--$boundary--\n end of the multi-part"; 
$titre="Feedback Form Result";


$to=$webmaster_email;
mail($to, $titre, $message, $headers);


You should also use $_GET or $_POST instead of $_REQUEST in your code (depending on the method you used in your form).
 
Last edited:
well heres my form
<h1>Contact Us</h1>
<form action="send_mail.php" method="POST" id="test" onsubmit="MM_validateForm('firstname','','R','email','','RisEmail','comments','','R');return document.MM_returnValue">
<input class="text" type="text" name="firstname" id="firstname" placeholder="First name*">
<input class="text" type="text" name="email" id="email" placeholder="Email*">
<input class="text" type="text" name="website" id="website" placeholder="Website*"><br/>
<textarea name="comments" id="comments" rows="5" placeholder="Message*"></textarea>
<div style='margin-top:20px'>
<div id='hOOman'></div>
<input id='captchaSubmit' type='submit' value=' Send ' disabled=true>
</div>


</form>
 
Try this, this should work:

<?php


/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "[email protected]";




$contacts_page = "contacts.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";




$firstname = $_POST['firstname'];
$email = $_POST['email'];
$website = $_POST['website'];
$comments = $_POST['comments'];


function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject, $str)) {
return true;
} else {
return false;
}
}


if (!isset($_POST['email'])) {
header("Location: $contacts_page");
}elseif (empty($firstname) || empty($email) || empty($comments)) {
header("Location: $error_page");
}elseif (isInjected($email_address)) {
header("Location: $error_page");
}else {
srand((double) microtime() * 1000000);
$boundary = md5(uniqid(rand()));
$texte .= "<center>Feedback Form Result</center>";




$headers = 'From: ' . $firstname . ' <' . $email . '>' . "\r\n" .
'Reply-To: ' . $email . '' . "\r\n";
$headers .="MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/alternative;boundary=$boundary\n";








$message = "\nThis is a multi-part message in MIME format.";
$message .="\n--$boundary\nContent-Type: text/html;charset=\"iso-8859-1\"\n\n";
$texte = "Firstname: $firstname <br/>
Email: $email <br/>
Website: $website <br/>
Comment: $comments <br/>";
$message .= $texte;
$message .="\n--$boundary--\n end of the multi-part";
$titre = "Feedback Form Result";




$to = $webmaster_email;
mail($to, $titre, $message, $headers);
header("Location: $thankyou_page");
}
?>
 
Last edited:
Does the code provided above work ? Otherwise i will create it from scratch and give you, i didn't have time yesterday to check that function but probably something is wrong in the rest of the code.

Thanks !
 
Does the code provided above work ? Otherwise i will create it from scratch and give you, i didn't have time yesterday to check that function but probably something is wrong in the rest of the code.

Thanks !

still not working.,when i fill all the forms it will redirect me to error_page.html

heres the live view http://roiancuares.cuccfree.org/contacts.html
 
I have modified the send_mail.php page for your requirements and tested, it works on my side. The main problem was that comments string was $_REQUEST['comments'] NOT $_REQUEST['message'] as is your form, that's why you were redirected to error page, now works fine.

PHP:
<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "[email protected]";


/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$contacts_page = "contacts.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";


/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$firstname = $_REQUEST['firstname'] ;
$email = $_REQUEST['email'] ;
$website = $_REQUEST['website'] ;
$comments = $_REQUEST['message'] ;


/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}


// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email'])) {
header( "Location: $contacts_page" );
}


// If the form fields are empty, redirect to the error page.
elseif (empty($firstname) ||empty($email) || empty($comments)) {
header( "Location: $error_page" );
}


// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}


// If we passed all previous tests, send the email then redirect to the thank you page.
else {

// Email content
$content = "Firstname: $firstname ( $email ) \r\n";
$content .= "Website: $website \r\n";
$content .= "Comments: \n$comments  \r\n";

// Additional headers
$headers .= 'To: Webmaster <'.$webmaster_email.'>' . "\r\n";
$headers = 'From: '.$firstname.' <'.$email.'>' . "\r\n";
mail($webmaster_email, "Feedback Form Results", $content, $headers);

header( "Location: $thankyou_page" );
}  
?>
 
I have modified the send_mail.php page for your requirements and tested, it works on my side. The main problem was that comments string was $_REQUEST['comments'] NOT $_REQUEST['message'] as is your form, that's why you were redirected to error page, now works fine.

PHP:
<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "[email protected]";


/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$contacts_page = "contacts.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";


/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$firstname = $_REQUEST['firstname'] ;
$email = $_REQUEST['email'] ;
$website = $_REQUEST['website'] ;
$comments = $_REQUEST['message'] ;


/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}


// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email'])) {
header( "Location: $contacts_page" );
}


// If the form fields are empty, redirect to the error page.
elseif (empty($firstname) ||empty($email) || empty($comments)) {
header( "Location: $error_page" );
}


// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}


// If we passed all previous tests, send the email then redirect to the thank you page.
else {

// Email content
$content = "Firstname: $firstname ( $email ) \r\n";
$content .= "Website: $website \r\n";
$content .= "Comments: \n$comments  \r\n";

// Additional headers
$headers .= 'To: Webmaster <'.$webmaster_email.'>' . "\r\n";
$headers = 'From: '.$firstname.' <'.$email.'>' . "\r\n";
mail($webmaster_email, "Feedback Form Results", $content, $headers);

header( "Location: $thankyou_page" );
}  
?>

it works it will redirect to thank_you page.,the only problem is it wont send any email.,is it because of the hosting? or do you guys know any other way of testing form without hosting?
 
That's because of the gmail adress. Try with an hotmail account or use MIME format in your mail because Gmail needs a strict MIME format to accept a mail.
 
Hello,

I've tested it using a gmail address too and worked for me, probably you have any problem with mail function in PHP of your webhost, maybe is disabled ? or check SPAM folder.

I suggest you to try this on some free webhosts such as 000webhost.com
 
That's because of the gmail adress. Try with an hotmail account or use MIME format in your mail because Gmail needs a strict MIME format to accept a mail.

i also tried the form you give me but it doesnt send an email as well.,i think theyre is problem with hosting.,free hosting doesnt support email i think
 
Hello,

I've tested it using a gmail address too and worked for me, probably you have any problem with mail function in PHP of your webhost, maybe is disabled ? or check SPAM folder.

I suggest you to try this on some free webhosts such as 000webhost.com

free hosting doesnt support email i guess.,are u using a premium one? when testing it.,it seems that it didnt work on me
 
Hmm, as I tested a while ago, 000webhost sent emails with the free plan. I am sure the problem is from your webhost, I've tested my code and worked for me, try to change your webhost or first contact the new webhosting provider to ask about mail function, or contact your current webhost company to enable sending emails for you.
 
Hmm, as I tested a while ago, 000webhost sent emails with the free plan. I am sure the problem is from your webhost, I've tested my code and worked for me, try to change your webhost or first contact the new webhosting provider to ask about mail function, or contact your current webhost company to enable sending emails for you.

thank you so much DeJohn.,its working now.,the hosting is the problem.
 
Back
Top