E-Mail Form and form values - php - php

I am trying to set a php mail form into my web site. First off all, I wanted to keep form values after submitting. if a user leave empty fields and press submit button, that user will has to write same things again. I was managed to do it with the structure <?php if (isset($_POST['Fields'])){echo htmlentities($_POST['Fields']); }?> However, I linked my form and php mail codes each other, after submitting form values are disappearing. I haven't managed to do it yet. I don't have knowledge about php. There are the codes:
My Form (inside contact.php):
<form method="post" action="mail.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" value="<?php if (isset($_POST['Name'])){echo htmlentities($_POST['Name']); }?>" />
<label for="Subject">Subject:</label>
<input type="text" name="Subject" id="Subject" value="<?php if (isset($_POST['Subject'])){echo htmlentities($_POST['Subject']); }?>"/>
<label for="Phone">Phone:</label>
<input type="text" name="Phone" id="Phone" value="<?php if (isset($_POST['Phone'])){echo htmlentities($_POST['Phone']); }?>"/>
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" value="<?php if (isset($_POST['Email'])){echo htmlentities($_POST['Email']); }?>"/>
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message">
<?php if (isset($_POST['Message'])){echo htmlentities($_POST['Message']); }?>
</textarea>
<input type="submit" name="submit" value="Send" class="submit-button" />
</form>
My php file (mail.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'];
$subject = $_POST['Subject'];
$phone = $_POST['Phone'];
$visitor_email = $_POST['Email'];
$message = $_POST['Message'];
//Validate first
if(empty($name)||empty($visitor_email)||empty($message))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
if(IsInjected($visitor_email))
{
header('Location: contact.php#jump'); // I'm gonna writing new stuff here
exit;
}
$email_from = 'updatedmail#mail.com';//<== update the email address
$email_subject = "Email from your web site";
$email_body = "You have received a new message via the web mail form \n".
"Name: $name \n".
"E-Mail: $visitor_email \n".
"Subject: $subject \n".
"Message: $message \n".
"Phone Number: $phone \n".
$to = "mymail#mymail.mail";//<== 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.php#jump'); // I'm gonna writing new stuff here
// 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;
}
}
?>

you have to include mail.php in contact.php. So in contact.php, on top write
<?php include("mail.php");?>
And if mail.php inside some folder include that folder name too like below:
<?php include("foldername/mail.php");?>
And remove mail.php from action="mail.php"
Let me know, if it's not working. :)

Related

HTML form PHP email not sending [duplicate]

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;
}
}
?>

Cannot post my php file, using xampp

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>

PHP - getting error message on submit when no error is present

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)) )

HTML - PHP contact form 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;

Unknown Sender or different email address in email header

I am trying to setup a new contact form. I am able to receive the email and the message correctly, but the reply-to is either showing up as (unknown sender) or my username at my nameserver, ex: coggi132#rs14.websitehostserver.net. I've looked at this for 4 hrs now, removing and adding in differnet things I've seen online. It does work if I hardcode an email address into the mail(..."$visitor_email") section. Thanks
Here is the html:
<form id="form" action="../assets/form-to-email.php" method="post">
<p><label class="required" for="namet">Name</label>(required)<br /><input name="name" id="name" type="text" required/>
<input type="text" style="display:none;" id="zip" name="zip" placeholder="Leave this field blank" autocomplete="off"></p>
<p><label class="required" for="mailt">E-mail</label>(required)<br /><input name="email" id="email" type="text" required/></p>
<p><label for="phone">Phone</label><br /><input name="phone" id="phone" type="text" required/></p>
<p><label class="required" for="message">Message</label>(required)<br /><textarea name="message" id="message" required></textarea></p>
<p><input class="btn_m" type="submit" name="submit" value="Submit Form" /></p>
</form>
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'];
$phone = $_POST['phone'];
$email_subject = "New Message From NovaWebDev.com Form";
$email_body = "You have received a new message from $name.\n
Phone Number: $phone \n
Here is the message: $message \n".
$to = "info#novawebdev.com";//<== Website's email address
$headers = "From: $visitor_email \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body, $headers);
//done. redirect to success page.
header('Location: /index.php/shared/email_success');
// 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;
}
}
?>
Use this;
$headers = 'From: ' . $visitor_email . "\r\n";
$headers .= 'Reply-To: ' . $visitor_email . "\r\n";
I figured this out. My email input was named "mail", while my php was "email". Thanks for the help guys.

Categories