This question already has an answer here:
change sender email address in php form to recipient
(1 answer)
Closed 7 years ago.
When the form is submitted I would like to change the name of the sender. Right now it just shows my test domain name as the sender when I get the email.
<?php
if ($_POST["submit"]) {
$result='<div class="alert alert-success">Form submitted</div>';
if (!$_POST['name']) {
$error="<br />Please enter your name";
}
if (!$_POST['email']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['comment']) {
$error.="<br />Please enter a comment";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were error(s) in your form:</strong>'.$error.'</div>';
} else {
if (mail("email#email.com", "Comment from website!", "Name: ".$_POST['name'],."
Email: ".$_POST['email']."
Comment: ".$_POST['comment'])) {
$result='<div class="alert alert-success"><strong>Thank you!</strong> I\'ll be in touch</div>';
} else {
$result='<div class="alert alert-danger"><strong>Sorry, there was an error sending your message. Please try again later.</strong></div>';
}
}
}
?>
Form
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 emailForm">
<h1>My email form</h1>
<?php echo $result; ?>
<p class="lead"> Please get in touch - I'll get back to you as soon as I can.</p>
<form method="post">
<div class="form-group">
<label for="name">Your Name:</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" value="<?php echo $_POST['name']; ?>" />
</div>
<div class="form-group">
<label for="email">Your Email:</label>
<input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $_POST['email']; ?>" />
</div>
<div class="form-group">
<label for="comment">Your Comment:</label>
<textarea class="form-control" name="comment"><?php echo $_POST['comment']; ?></textarea>
</div>
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
</form>
</div>
</div>
</div>
This is my first contact form, and would really appreciate the help!
Thanks in advance!
As stated in the PHP Documentation
$to = 'user#example.com';
$subject = 'Comment from website!';
$message = 'hello';
$headers = 'From: your_name#example.com' . "\r\n" .
'Reply-To: your_name#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Related
I tried this in my code and I entered the right secret key just didn't want to put it out there but everytime I submit my form even with it checked it shows up with the error that it isn't filled out, without the captacha stuff in the form it works just fine. Can someone please help me fix this issue I would like it to make sure you filled it out before it sends it to my email!!
<h2 class="text-center" id="whatwedo">Contact form</h2><hr class="titlehr"></div></div><br>
<?php
if (isset($_POST["submit"])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$description = $_POST['description'];
$captcha = $_POST['g-recaptcha-response'];
$from = $fullname;
$to = 'mcgarrywebdesign#gmail.com';
$subject = 'Contact Form';
$body = "From: $fullname\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n description: $description";
// Check if name has been entered
if (!$_POST['fullname']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone number';
}
if (!$_POST['subject']) {
$errSubject = 'Please enter the subject';
}
if (!$_POST['description']) {
$errDescription = 'Please enter the description';
}
if(!$captcha){
$errcaptcha = "Please check the the captcha form";
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errSubject && !$errDescription && !$errcaptcha) {
$secretKey = "secret key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$result = "You are spammer ! Get the #$%K out";
} else {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}}
?>
<?php echo "<p class='text-danger'>$result</p>";?>
<div class="row">
<div class="col-md-12 col-lg-6"> <form action="contact.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Full Name" name="fullname">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Email Address</label>
<input type="email" class="form-control" id="exampleInputPassword1" placeholder="Enter Email" name="email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone Number</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Phone Number" name="phone">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Subject" name="subject">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<label for="exampleTextarea">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3" name="description" placeholder="Description"></textarea>
<?php echo "<p class='text-danger'>$errDescription</p>";?>
</div>
<br>
</div></div>
<div class="g-recaptcha text-center mx-auto d-block" data-sitekey="6LdhyXcUAAAAANrj8qTSLKcrbjVX6ij07Dqw0awe"></div>
<?php echo "<p class='text-danger'>$errcaptcha</p>";?>
<div class="row"><div class="col-lg-12"><button type="submit" name="submit" value="send" class="btn btn-primary mx-auto d-block" style="width: 190px !important;height: 60px !important;font-size: 25px;">Submit</button></div></div></form>
</div>
This is the code for the form.
<?php
if ($_POST) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$checkbox = '';
if (isset($_POST['checkbox'])) {
$checkbox = 'Yes';} else{
$checkbox = 'No' ;}
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'imvael#gmail.com, vnikolic1#cps.edu';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Company: $company\n Phone: $phone\n Opt In?: $checkbox\n Message:\n $message";
$headers = 'From: webmaster#bradfordsystems.com' . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if name has been entered
if (!$_POST['phone']) {
$errName = 'Please enter your phone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// then after the posting of the form data
// validation
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && isset($_POST['url']) && $_POST['url'] == '') {
if (mail ($to, $subject, $body, $headers)) {
header("Location: thankyou.php"); /* Redirect browser */
exit();
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
This is the form itself
<div class="wrapper">
<div class="row">
<div class="col _66">
<?php echo $result; ?>
<form role="form" name="contactForm" id="contactForm" method="post" action="contact.php#contactForm">
<p>
We welcome your feedback and inquiries. Please use the form below to get in touch.
</p>
<div class="row">
<div class="col">
<input type="text" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="col">
<input type="email" id="email" name="email" placeholder="Company Email" value="<?php echo htmlspecialchars($_POST['email']); ?>" required>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" id="company" name="company" placeholder="Company" value="<?php echo htmlspecialchars($_POST['company']); ?>" required>
</div>
<div class="col">
<input type="tel" id="phone" name="phone" <?php echo htmlspecialchars($_POST['phone']); ?> placeholder="Phone" required>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="number" id="zipcode" name="zipcode" placeholder="Zip Code" value="<?php echo htmlspecialchars($_POST['zipcode']); ?>">
</div>
<div class="col">
<input id="checkBox" type="checkbox"> <span id="optInText">YES, I want a Free Workspace Evaluation!</span>
</div>
</div>
<div class="row">
<div class="col">
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
</div>
</div>
<div class="row">
<div class="col submit-col">
<p>Questions or Comments?</p>
<textarea id="message" name="message" placeholder="Enter your questions or comments here" style="height:200px"> <?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<button class="btn btn-dark hvr-underline-from-left" name="submit" type="submit" value="Send">Submit Request</button>
</div>
</div>
</form>
</div>
</div>
</section>
I am writing a contact form script from scratch The code is working correctly, but I am unable to prevent resubmissions if they user refreshed the page or press the submit buton more then once.
Also is it a bad practice to use self submitting contact forms?
This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 5 years ago.
Fixed:
There was a small error in my code, I had changed the "type" to "text" somehow in the email input, where as it should have been
type="email"
and not
type="text"
I have made a php contact form which I use on website I make and all works fine.
I thought the form was checking the email address had been entered but I have now realised people can just put their name in the email input and not their full email address (i.e with an # symbol and a .com or .co.uk at the end)
I am not so great at PHP so I have had a go but emails from the form where I haven't put a valid email address in still seem to go through despite the statements I added
I tried to define some error variables and set with empty values as follows:
$nameErr = $emailErr = $genderErr = $websiteErr = "";
and then adding these if else statements to my form php:
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
So the my code looks as follows:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$message = ($_POST['message']);
$from = ($_POST['email']);
$to = 'me#myemailaddress.co.uk';
$subject = "Enquiry from Visitor " . $name;
$human = ($_POST['human']);
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
?>
<div class="container-fluid">
<div class="about-msg">
<h2>CONTACT US</h2>
<div class="container-fluid contactform">
<div class="col-md-7 contactform-padding">
<br>
<h1>GET IN TOUCH</h1>
<p>Please drop us a message if you have any enquires. We always aim to reply
within 24 hours.</p>
<?php
if (isset($_POST['submit']) && $human == '4') {
if (mail ($to, $subject, $message, $headers)) {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
echo '<p>Thanks for getting in touch. Your message has been sent & we
will get back to you shortly!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if (isset($_POST['submit']) && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
<form method="post" action="contact-us.php" data-ajax="false" method="POST"
enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-6">
<label class="control-label " for="name">
<b>NAME</b>
</label>
<input class="form-control" id="name" name="name" type="text"
placeholder="name">
</div>
<div class="form-group col-md-6">
<label class="control-label requiredField" for="email">
<b>EMAIL</b>
<span class="asteriskField">
*
</span>
</label>
<input class="form-control" id="email" name="email" placeholder="email"
type="text"/>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="control-label requiredField" for="contact-subject">
<b>SUBJECT</b></label>
<input type="text" name="subject" class="contact-subject form-control"
id="contact-subject" placeholder="subject">
</div>
</div>
<div class="form-group">
<label class="control-label " for="message">
<b>MESSAGE</b>
</label>
<textarea class="form-control" cols="40" id="message" name="message"
rows="10" style="height: 275px !important;"></textarea>
</div>
<div class="form-group">
<label><b>*What is 2+2? (Anti-spam)</b></label>
<input name="human" placeholder="Type Here"></div>
<br>
<button class="btn btn-success" name="submit" type="submit" value="submit">
SEND
</button>
</form>
</div>
However this lets me send an email despite putting my email address as just my name!
Could someone please take a look and see if I have put the code in the right place or make a suggestion as to how to fix it? I am just learning PHP so I may have made an error in my code or maybe need to take the if statement out for the submit?
Thanks for any help!
Use PHP Filter Validation
$email_b = "me#myemailaddress.co.uk";
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_b) email address is considered valid.\n";
} else {
echo "This ($email_b) email address is considered invalid.\n";
}
I think you want the user to enter a valid email address, right?
You could change the type to email
<input class="form-control" id="email" name="email" placeholder="email" type="email"/>
Well you could either go for regex but this is also an option
Not sure what is causing the error. I have tried several times but continue to get the error. Any help welcome. Thank you.
Here is my form:
<form class="form-horizontal" role="form" method="post" action="modalcontact.php">
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<input type="text" class="form-control" id="name" name="name" placeholder="NAME" value="">
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<input type="email" class="form-control" id="email" name="email" placeholder="EMAIL" value="">
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<textarea class="form-control" rows="4" name="message" placeholder="MESSAGE"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
</div>
</div>
</form>
Here is the PHP under modalcontact.php file in my hosting server:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'L|D Contact Form';
$to = 'myemail#email.com';
$subject = 'Message from L|D Contact ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
echo '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
echo '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Try this code replace the from with header:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: abc#gmail.com';
I tested your code and it works fine. Then what's problem on your server...
Below I have my html and php coding. I've racked my brain trying several different options, but what I am really looking for is something simple and secure that either reloads to my original page showing sent message. As of right now I get this error message when submitting my form. Someone please point me in the right direction, any feedback is appreciated.
HTML CODING(index.html)
<html>
<head><title></title></head>
<body>
<div id="wrapper2">
<div id="newsletter" class="container">
<div class="title">
<img src="images/port-formdivide.png" alt="" width="800" height="60" />
<h2>Contact Me</h2>
<span class="byline">Please complete the entire form.</span> </div>
<div class="content">
<form action="form.php" method="POST" enctype="multipart/form-data" target="_self">
<div class="row half">
<div class="6u">
<input name="name/company" id="name/company" type="text" class="text" maxlength="30"
placeholder="Name/Company" value="" />
</div>
<div class="6u">
<input name="email" id="email" type="text" class="text" maxlength="30"
placeholder="Email" value="" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" value=""></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input name="Send Message" type="submit" class="button submit" value="Send Message" />
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
PHP CODING(form.php)
<?php
// Email Variables
$to = 'inquiries#triston-thomas.com';
$subject = 'New Inquiry';
/* Data Variables */
if ($_POST['parse_var'] == "form"){
$emailTitle = 'New Message Triston';
$yourEmail = 'inquiries#triston-thomas.com':
$name/company = $_POST['name/company'];
$email = $_POST['email'];
$message = $_POST['message'];
echo ("Name/Company:" . $name/company);
echo ("<br />Email:" . $email);
echo ("<br />Message:" . $message);
function response($name/company);
{
echo ("Thank You " . $name/company);
}
header ("Location: index.html");
mail ($to, $subject, $message, "From: ".$name/company);
echo "Your Inquiry Has Been Sent";
// Set Required Fields
$required_ fields = array("$name/company","$email","$message");
// Set Error Messages
$error_message = array("$name/company" => 'Please Enter Your Name or Company Name In
The Field',
"$email" => 'Valid Email Address Is Required',
"$message" => 'Please Enter Message In The Field',);
/*Behaviors*/
if ($_POST)
{
mail($to, $subject, $message, $header);
}
if(isset($_POST['message']))
{
echo $_POST['name/company'];
echo "<br />";
echo $_POST['email'];
echo "<br />";
echo $_POST['message'];
}
if(empty($_POST['name/company']))
{
echo "Name Field is Required";
}
if(empty($_POST['email']))
{
echo "Email Field is Required";
}
if(empty($_POST['message']))
{
echo "Message Field is Required";
}
$body = <<<EOD
<br><hr><br>
Name/Company: $name/company <br />
Email: $email <br />
Message: $message <br />
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail("$yourEmail", "$emailTitle", "$body", "$headers");
$sent = "Thank You, Your Inquiry Has Been Sent";
echo $result;
enter code here
}
?>