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.
Related
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 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. :)
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;
I have a basic contact form for a web site I'm working on. I use HTML and PHP for do that.
The HTML:
<form action="enviarc.php" method="post" name="contacto" target="pop" id="cntct-c" onsubmit="pop = window.open('about:blank','pop', 'width=400,height=250');this.submit(); this.reset(); return false;">
<p>
<input name="nombre" type="text" id="campos-n" class="form-imputs" size= "36" placeholder="Nombre *"/>
</p>
<p>
<input name="email" type="text" id="campos-em" class="form-imputs" size="36" placeholder="Email *"/>
</p>
<p>
<input name="telefono" type="text" id="campos-te" class="form-imputs" size="36" placeholder="Teléfono"/>
</p>
<p>
<input name="celular" type="text" id="campos-ce" class="form-imputs" size="36" placeholder="Celular"/>
</p>
<p>
<input name="ciudad" type="text" id="campos-ci" class="form-imputs" size="36" placeholder="Ciudad"/>
</p>
<p>
<textarea name="mensaje" id="campos-m" class="form-imputs" cols="36" rows="6" placeholder="Mensaje *" style="height:100px;"></textarea>
</p>
<input name="Submit" type="submit" class="form-buttons" value="Enviar"/>
<input name="Clean" type="reset" class="form-buttons" value="Limpiar">
</form>
And the PHP:
<?php
if(isset($_POST['email'])) {
$email_to = "xxx#xxx";
$email_subject = "Contacto desde el formulario de xxxxxx";
$nombre = $_POST['nombre'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$celular = $_POST['celular'];
$ciudad = $_POST['ciudad'];
$mensaje = $_POST['mensaje'];
$email_message .= "Nombre: ".($nombre)."\n";
$email_message .= "Email: ".($email)."\n";
$email_message .= "Telefono: ".($telefono)."\n";
$email_message .= "Celular: ".($celular)."\n";
$email_message .= "Ciudad: ".($ciudad)."\n";
$email_message .= " \n";
$email_message .= "Mensaje: ".($mensaje)."\n";
//$email_message .= "Enviado el " . date('d-m-Y', time());
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
// 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;
}
}
?>
<!-- include your own success html here -->
Su mensaje fue enviado correctamente.<br>XXXXXXXX estara en contacto con usted.
<?php
}
?>
I'm not a php developer, i'm a web designer so i use some code from tutorials. The issues is that the code works great in firefox, send the email and works fine but in Google Chrome the form breaks and dont send the email. When click the "submit" button the pop up windows open and shown the "mesage sent" text, but the email doesn't send.
I've searched arround the web and i've read about a problem between google chrome and PHP "$_POST" and "submit" but i cant fix the issues.
Any suggestion is welcome or a link to a functionally php form tutorial what works in most browsers (although I read a lot and can not find one that works), is for a job and the time is finish me :) like every time...
I answer my own question. Maybe the answer is useful for other person.
The issues came from the target to open the "file.php". I use a JavaScript line in the tag to open the "comments sent" message in a pop window. Google Chrome have a problem with that, so I fix the issue terget the "thanks page" on the self page.
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";