I am sorry for what is a very basic question but I am very new to web design and I am just trying to get my form to work with php.
The form fills in fine, and I receive an email but I don't get any body text. Instead of telling me what the message is I get this:
You have received a new message from the user fgafdfa.
Here is the message:
info#sodium3.com
As you can see the name is there (just typed gibberish) however the message is missing, instead I get my own email address appearing...
Here is the HTML for the form:
<div id="form">
<form action="php/send_form_email.php" method="post" >
<span>Name</span>
<input type="text" name="name" class="name" />
<span>Email</span>
<input type="text" name="email" class="email"/>
<span>Message</span>
<textarea class="message"></textarea>
<input type="submit" name='submit' value="submit" class="submit">
</form>
</div>
Here's the php:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'info#sodium3.com';//
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "info#sodium3.com";//
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: ../index.htm');
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;
}
}
?>
Can anyone help?
Two errors:
See the missing name in the HTML for the textarea. Corrected code:
<textarea name="message" class="message"></textarea>
For the code you don't end your assignment to $email_body with a semicolon but appended to it by a dot. Just a bit of luck that you didn't end up with a parse error. Corrected code:
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "info#sodium3.com";
Related
This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I am trying to send an email upon submiting completed form from my site. however when ever i hit submit it only displays my php file as text and does not send the email.
below is all of my scripts could you please help me figure out why it wont send the email and display the thank you page?
My HTML File
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'>Enter Name: </label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Enter Email Address:</label>
<br>
<input type="text" name="email">
</p>
<p>
<label for='message'>Enter Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" name='submit' value="submit">
</form>
<br>
<br>
<br>
<br>
<script language="JavaScript">
var frmvalidator = new Validator("myemailform");
frmvalidator.addValidation("name", "req", "Please provide your name");
frmvalidator.addValidation("email", "req", "Please provide your email");
frmvalidator.addValidation("email", "email", "Please enter a valid email address");
</script>
My php File
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'nmckinney#chaoscomputerpro.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "nicholas.mckinney#outlook.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
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;
}
}
?>
I'm trying to set up an HTML contact form on my website that will use PHP to send the information entered into the form to my email address.
Unfortunately, I keep getting an error that says Cannot POST /form.php and I can't figure out why.
I looked through similar questions that were asked here on StackExchange and the number one cause seemed to be that the PHP file wasn't in the same folder as the index.html file. However, both of my files are in the same folder; newwebsite/index.html and newwebsite/form.php.
So, why could this be happening?
Here's my index.html form:
<form method='post' action='form.php'>
<label>Name: </label>
<input type='text' name='vistorName' id='form-name' placeholder="Type your name.." required>
<br>
<label>Email: </label>
<input type='email' name='vistorEmail' id='form-email' placeholder="Type your email.." required>
<br>
<label>Msg: </label>
<textarea type='text' name='vistorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
<br>
<input type='submit' action="submit" value='Submit' id='submit-button' required><br>
</form>
And here's my form.php file:
<?php
//Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];
//Structure of email that I will receive with the form info
$email_from = "myemail#email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
"Here is the message: \n $message".
//Sending to my email address and using the mail function
$to = "zcericola#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
//Validating the form
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(IsInjected($email))
{
echo "Bad email value!";
exit;
}
?>
//HTML confirmation that message was sent.
<html lang='en'>
<h1>Your email has been sent. Thank-you!</h1>
</html>
With a few modifications, your code worked on my server.
Per Akintunde's comment, your form has typos in the field names:
<html>
<form method='post' action='form.php'>
<label>Name: </label>
<input type='text' name='visitorName' id='form-name' placeholder="Type your name.." required>
<br>
<label>Email: </label>
<input type='email' name='visitorEmail' id='form-email' placeholder="Type your email.." required>
<br>
<label>Msg: </label>
<textarea type='text' name='visitorMsg' id='form-msg' placeholder="Type your message here..." required></textarea>
<br>
<input type='submit' action="submit" value='Submit' id='submit-button' required><br>
</form>
Your "form.php" checks the e-mail address after attempting to send the e-mail. Also, html comments are identified differently than php comments. I made those two changes.
<?php
//Pulling values that were entered into the form.
$name = $_POST['visitorName'];
$email = $_POST['visitorEmail'];
$message = $POST['visitorMsg'];
if(IsInjected($email))
{
echo "Bad email value!";
exit;
}
//Structure of email that I will receive with the form info
$email_from = "myemail#email.com";
$email_subject = 'New Contact Form Message';
$email_body = "You have received a new message from $name. \n".
"Here is the message: \n $message".
//Sending to my email address and using the mail function
$to = "zcericola#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
//Validating the form
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;
}
}
?>
<html lang='en'>
<!-- HTML confirmation that message was sent. -->
<h1>Your email has been sent. Thank-you!</h1>
</html>
I have a problem getting the data from my mail form being sent correctly in an email.
It does something strange with the email message.
Original form message:
Enter Name: Fake Name
Enter Email Address: fake.name#doesnotexist.com
Enter Message: Email test number 4(four).
Submit
_____________________________________________
The email message I received:
From: <fake.name#doesnotexist.com>
Subject: <New Form submission>
Reply to: <warnerheston#sungraffix.net>
To: Me <web289portfolio#sungraffix.net>
_____________________________________________
You have received a new message from the user Fake Name.
Here is the message:
Email test number 4(four).web289portfolio#sungraffix.net
_____________________________________________
The email in the message body is "(four).web289portfolio#sungraffix.net" and is underlined/hyperlinked... STRANGE!!
IT IS NOT SUPPOSED TO HAVE ANY EMAIL ADDRESS IN THE MESSAGE BODY AT ALL. THE USER DID NOT TYPE AN EMAIL ADDRESS AT THE END OF THE MESSAGE. SOMEHOW, THE PHP CODE IS DROPPING THE DESTINATION EMAIL ADDRESS INTO THE MESSAGE BODY AND ATTACHING THE LAST "WORD" OF THE USER'S MESSAGE TO THAT EMAIL ADRESS WHEN IT SENDS IT.
Can someone tell me what I am doing wrong?
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact-form-incomplete.html');
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = $_POST['email']; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "web289portfolio#sungraffix.net";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: contact-thankyou.html');
// Function to validate against any email injection attempts
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;
}
}
?>
This is the HTML Form:
<form method="post" name="myemailform" action="form-to-email.php">
<p>
<label for='name'><span class='form'>Enter Name:</span></label><br>
<input type="text" name="name" size="36">
</p>
<p>
<label for='email'><span class='form'>Enter Email Address:</span></label><br>
<input type="email" name="email" size="36">
</p>
<p>
<label for='message'><span class='form'>Enter Message:</span></label> <br>
<textarea name="message" cols='48' rows='9' maxlength='300'></textarea>
</p>
<input type="submit" name='submit' value="submit"> <input type="reset">
</form>
Look carefully at your code on $email_body, you actually concatenate the $email_body with $to. the concatenation . at the end of your $email_body should be ;.
so I have built this email form, which is supposed to display the error message 'name and email are mandatory' if the name or/ and email are not submitted. However I am getting the same message if they are. How can that be fixed?
<?php
if(!isset($_POST['submit-enquiry']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$guest_email = $_POST['email'];
$mobile = $_POST['mobile'];
$message = $_POST['enquiry-message'];
//Validate first
if(empty($name)||empty($guest_email))
{
echo '<script language="javascript">';
echo 'alert("Name and email are mandatory")';
echo '</script>';
exit;
}
if(IsInjected($visitor_email))
{
echo '<script language="javascript">';
echo 'alert("Bad Email Value")';
echo '</script>';
exit;
}
$email_from = $guest_email;//<== update the email address
$email_subject = "Enquiry from $name";
$email_body = "Name: $name. \n". "Mobile: $mobile .\n". "Message: $message. \n";
$to = "my_email";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $guest_email \r\n";
/ /Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: enquiry.php');
// Function to validate against any email injection attempts
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;
}
}
?>
The html of the fprm:
<div id="enquiry-form">
<form method="post" name="enquiry-form" action="" target="_self">
<span class="short-input" id="name">
<h6>name</h6>
<input type="text" name="name">
</span>
<span class="short-input" id="mobile">
<h6>mobile</h6>
<input type="text" name="mobile">
</span>
<span class="long-input" id="email">
<h6>e-mail</h6>
<input type="text" name="email">
</span>
<span class="long-input" id="enquiry-message">
<h6>enquiry</h6>
<textarea name="enquiry-message"></textarea>
</span>
</div>
<div id="contact-info">
<h2>Contact Details</h2>
</div>
<button type="submit" id="submit-enquiry" name="submit-enquiry">send</button>
</form>
try
if( (empty($name)) || (empty($guest_email)) )
for more infos : php operation precedence
Try
if( $name=='' || $guest_email=='' )
empty in some cases can lead to unexpected results. My experience led me to say that for a form validation it is better to check if the variable is empty in this way. Then you check the content of the variable (to see for example if it is a valid email) so this is safe enough for me.
If your code still doesn't work then it is necessary to do further investigations.
if( empty(trim($name)) || empty(trim($guest_email)) )
I have searched here and in google how to make a contact form to work and I havent been able.
I downloaded a "php contact form" but I dont know how to make it work. I know there have been a lot asked question about this but please help me out. I am stuck here :/
This is the HTML code:
<form action="form-to-email.php" name="myemailform" method="post">
<div>
<span>Name</span>
<input type="text" name="name" value="" placeholder="Your Name">
</div>
<div>
<span>Email</span>
<input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="example#domain.com">
</div>
<div><textarea name="message" placeholder="Message"></textarea></div>
<div class="code">
<button><input type="submit" name='submit' value="Send"></button>
</div>
<i class="clear" style="display: block"></i>
</div>
</form>
And here is the "free PHP form code" I downloaded from a website:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = 'myEmail#domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "myEmail#domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
// Function to validate against any email injection attempts
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;
}
}
?>
Do I need to do something in mysql or do something more at all?
Thanks for the help!
Here "Here is the message:\n $message". <= see the dot? That's supposed to be a semi-colon.
That, is the reason why OP's code is not working.
Add error reporting to the top of your file(s) which will help find errors, which it would have.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
everything looks correct, just add exit after redirect.
header('Location: thank-you.html');
exit;