Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Hi guys this is my first question on stackoverflow.
I'm an amateur web designer designing a mostly static website..I need to create a contact form for user queries.What's the best approach for this task?
1)php's mailto function?
2)form data stored in a text file or spreadsheet?
3)database-connected(not preferred)
Any help would be really appreciated!!
To create a simple contact us form in php,you dont need to create any database. You have to use mail() function in PHP
You can refer following links to built simple contact us form :-
http://www.phpeasystep.com/phptu/8.html
http://www.freecontactform.com/email_form.php
OR
page1.php
<h2>Your Title</h2>
<form action="receiving.php" method="POST">
Name:<br><input type="text" name="name" size="40" /><br><br>
Email:<br><input type="text" name="email" size="40" /><br><br>
Phone:<br><input type="text" name="phone" size="40"><br><br>
Message:<br><textarea name="message" rows="3" cols="31" > </textarea><br><br>
<input type="submit" name="submit" value="Submit" />
<br><br>
</form>
receiving.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if(isset($_POST['submit']))
{
$from_add = "contactform#yourwebsite.com";
$to_add = "yourname#yourwebsite.com";
$subject = "Your Subject Name";
$message = "Name:$name \n Email: $email \n Phone: $phone \n
Message: $message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent";
}
}
print "<p> Thank you $name for your message,
we will be in contact shortly. Click here
to continue </p>" ;
?>
NOTE :- You cannot send mail from localhost, configure some other smtp at localhost eg : google,yahoo...
Sample code of contact form using mail function
<?php
if (isset($_POST['action'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($name == "" || $email == "" || $message == "") {
echo "All fields are required, please fill the form again.";
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail("youremail#yoursite.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<form action="" method="POST">
Your name:<br>
<input name="name" type="text" /><br>
Your email:<br>
<input name="email" type="text"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" name="action" value="Send email"/>
</form>
Simply build a html-form and send the data to a php-file. Database is not needed at all, unless you want to store all messages or IP.
A simple search will get you a lot of results.
In the phpfile you have two options: Use php's mail() as is, or try PHPmailer. The latter is a bit more complecated, but ends up less as spam, it sets headers and everything for you.
Especially when you mail from multiple pointer on your website, I recommend PHPmailer.
In reply to one of your comments:
All mailing happends on the server. PHP is a serverside language, all actions take place on the server, not on the users computer. For that reason, your code should always be on a server, or local with WAMP (or LAMP)
Related
This question already has answers here:
PHP email form shooting blank emails
(4 answers)
Closed 2 years ago.
I have a contact form on two different websites I have made for clients.
At around 8-9pm everyday a blank message is sent using the contact form and straight to my clients' respective email addresses.
PHP:
<?php
$name = $_POST['full-name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['full-name'];
$to = 'mobileguitarworkshop#hotmail.com';
if(!empty($_POST['field'])) die();
$email_from = 'mobileguitarworkshop#hotmail.com';
$email_subject = "Enquiry from $name.\n";
$body = "From: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to, $email_subject, $body, $headers);
header("Location: http://mobileguitarworkshop.co.uk/success.html");
exit();
?>
HTML:
<form action="contact.php" method="post" class="contact-form">
<label for="full-name">Name</label>
<input name="full-name" type="text" id="full-name" required>
<input type="text" id="field" name="field"/>
<label for="phone">Phone</label>
<input name="phone" type="tel" id="phone">
<label for="email">Email address</label>
<input name="email" type="text" id="email" required>
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<input name="send" type="submit" value="SEND" id="sendBtn">
</form>
I've tried adding 'required' to the Name and Email Address inputs to stop spammers, and also a hidden field that, if filled, directs them to 'success.html' without posting the message.
If anyone can explain why this is happening that would be great. The hosting service I'm using is 1&1 IONOS.
Thanks,
Jack
The spammers may be sending a request directly to the contact form endpoint, bypassing your form entirely. This means that required fields in the html wont do much to stop that. You'll need to check those properties on the backend to prevent those submissions. Something like this would work:
if(empty($_POST['full-name']) || empty($_POST['email'])) {
die();
}
If I were you, I'd also look into implementing a CSRF token. See How to properly add CSRF token using PHP
While we're talking, we really should sanitize the $_POST['message']; with something like the below to remove any questionable html content your users may have submitted:
$message = strip_tags($_POST['message']);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a website and the form in the code is not working up. What can I do to make it work and put it in action. Here is the form code....
<div class="form"; action="mail.php"; method="post">
<input class="input-text" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<input class="input-text" type="text" name="email" value="Your E- mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<textarea class="input-text text-area" name="message" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">Your Message *</textarea>
<input class="input-btn" type="submit" value="send message">
</div>
Here is the PHP code I am using to fetch data from form.
<html>
<body>
<?php
$errors = '';
$myemail = 'myemail#example.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The problem is that after entering data in the form, it is not proceeding any further.
Change the markup so that it is a <form> not a <div> and get rid of the semicolons.
<form class="form" action="mail.php" method="post">
...
</form>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I know this is a popular question. However no other questions can give me the answer I'm looking for.
Have a contact form (mail() being using. And I can't get it to send. A coder that helped me create it somehow got his to send, because I received a few messages in my inbox the mail was coded to send to. I copy and pasted the code, and I'm testing it locally, but it's not sending the mail.
Is the problem it's not sending because I'm testing it locally and its not live and hosted yet? or does the problem rely in my code, and if so, where?
***Not including the validation code, but I have it there...
FORM:
<form method="post" action="">
<input type="text" name="name" placeholder="*Name" value="<?php echo $_POST['name']; ?>">
<input type="tel" name="phone" placeholder="*Phone Number" value="<?php echo $_POST['phone']; ?>">
<input type="email" name="email" placeholder="*Email" value="<?php echo $_POST['email']; ?>">
<input type="text" name="invoice" placeholder="Invoice Number (optional)" value="<?php echo $_POST['invoice']; ?>">
<textarea name="comments" maxlength="500" rows="10" cols="10" placeholder="*Please enter your comments here..."><?php echo htmlentities($_POST['comments'], ENT_COMPAT,'ISO-8859-1', true);?></textarea>
<button type="submit">Submit</button>
</form>
PHP:
if(!empty($_POST)){
$POST = filter_post($_POST);
$invoice = array_splice($POST,3,1);
$MSG = check_empty($POST);
$email = test_input($_POST["email"]);
if(!array_filter($MSG)){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$MSG[] = "Invalid Email Format (test#provider.com)";
}
else{
$POST['invoice'] = $invoice['invoice'];
if(send_mail($POST)){
header('Location: messageSent.php');
}
else{
$MSG[] = "Email Failed. Please Try Again.";
}
}
}
}
function send_mail($POST){
extract($POST);
$to = '7servicerepairs#gmail.com';
$sbj = 'New Question For Se7en Service!';
$msg = "Name: $name \n Phone: $phone \n Email: $email \n Invoice #: $invoice \n Comments: $comments";
$headers = "From: $email";
return(mail($to, $sbj, $msg, $headers));
}
You will need an smtp server in your localhost. Otherwise you won't be able to send it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I used this post to make a simple ‘contact us’ form. The form collects a few pieces of information (email, name and a message) from your visitor and emails it to you.
Instead of redirecting to a thank you page, I'd prefer it to just print the thank you (or error) text in the same HTML form page.
How can this be done? Where do I need to change to be able to keep the user in the same page and show the confirmation message ("Thank you")?
Edit: Found a much simpler way to do this - with ajax.
Here is my suggestion.
1- Change your file name contact-form.html to contact-form.php
2- Added include line and edit your form action in contact-form.php:
<h1>Contact us</h1>
<?php include './contact-form-handler.php'; ?>
<form method="POST" name="contactform" action="#">
3- Edit your contact-form-handler.php, disable header and added your thank you with personal message, you can also added some layout and decoration that is left to your fantasy.
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
4- Last thing I will added condition to check if the form is submitted or not
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty... etc
}
5- (Optional) To re-fetch the posted values if failed I have added following to the form inputs (3 of them, each field got one with the respective field/variable name):
value="<?php if (isset($_POST['name'])) echo $name; ?>"
But in general When all this done, I will personally will go through the form and clean it up a little bit. Like if I am check the content in JavaScript it is not necessary to check if the fields are empty, so checking submission form is enough. I have not talk about form security yet against any kind of misuse, this just to make some thoughts when you come so far.
Complete code
Here is your final results, with extra things I done:
contact-form.php
<form method="POST" name="contactform" action="#">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $name; ?>">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $email_address; ?>"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message" value="<?php if (isset($_POST['message'])) echo $message; ?>"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
contact-form-handler.php
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])
)
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)
)
{
$errors .= "\n Error: Invalid email address";
}
if (empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. " .
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
//redirect to the 'thank you' page
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
$name = "";
$email_address = "";
$message = "";
}
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing a contact form for a website and am having trouble getting it to send the fields of my form on the actual e-mail.
The e-mails are sent and received successfully though.
Here is my HTML
<form action="mail.php" method="post" style="font-size:12px;">
<p>Name</p> <input type="text" name="name">
<p>Telephone</p><input type="text" name="phone" size="30" />
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="comments" rows="6" cols="25"></textarea><br />
<p>Best time to Contact You</p><input type="text" size="15" name="time" />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
and my PHP
<?php
$to = "myemailaddress";
$subject = "New Website Enquiry";
$message = "You have recieved a new enquiry";
$from = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['comments, phone, time'];
$headers = "From:" . $from;
$url = 'index.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
mail($to,$subject,$message,$headers);
?>
You need to learn basic PHP syntax.
$message = $_POST['comments, phone, time'];
is outright wrong. You cannot specify multiply array keys in a comma-separated list like that, let along within a STRING. The code should be more like:
$message = $_POST['comments'] . $_POST['phone'] . $_POST['time'];
Note that what you have is NOT a php-level syntax error. You're using a perfectly acceptable array key, which simply happens to not exist.
I found an syntax error in your code:
Change
$message = $_POST['comments, phone, time'];
to
$message = $_POST['comments'] . . $_POST['phone'] . $_POST['time'];
What else do you have a problem with?
<?php
$to = "myemailaddress";
$subject = "New Website Enquiry";
$message = "You have recieved a new enquiry";
$from = $_POST['email'];
$name = $_POST['name'];
$headers = "From:" . $from;
$url = 'index.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0;URL='.$url.'">'; mail($to,$subject,$message,$headers);
?>