php contact form mistake in code? - php

I am beginner in php and I tried to programe an easy contact form. My question is: whats wrong with my code? I cant seem to find any mistake with formating or anything... This is my code:
contact.php:
...page content...
<div class="contact">
<form action="send_mail.php" method="post">
<fieldset>
<label for="name">Name:</label>
<input type="text" name="name" placeholder="Enter your name" />
<label for="email">Email:</label>
<input type="email" name="email" placeholder="Enter your email address" />
<label for="message">Message:</label>
<textarea name="message" placeholder="What's on your mind?"></textarea>
<br>
<input type="submit" name="submit" value="Send message" />
</fieldset>
</form>
</div>
...page content...
send_mail.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: My website';
$to = 'someone#email.cz';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I am getting the "Something went wrong" message, which means taht mail() returned false but I dont know why. Thanks for any help!

This is a common problem with running scripts on your local computer. Most computers will not have an email server and be configured to send email from that server.
For the PHP mail function to work, your computer must have an email server set up.
You can either look at online hosting with email support or for local testing, install a test mail server.
There is a bit about non mac OS's at the end. I've had some luck with this in the past.
If your on windows, this is probably better: http://www.toolheap.com/test-mail-server-tool/

Your headers need to contain proper headers.
Using only From: My website stands at going to Spam.
Using an Email => From: $email\r\n will work better.
Give this a try, tested.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\n";
$to = 'your_email#somewhere.com';
$subject = 'My contact form message';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(empty($email)) {
die("Something went wrong, go back and try again!");
}
elseif (isset($_POST['email'])) {
mail($to,$email,$body,$headers);
echo "<p>Your message has been sent!</p>";
}
?>

1 . To made work this code.you should send your mail from any domain.
for example: you should have a live website thru that domain email id only send mail to
others
2 . use PHP-MAILER coding for sending mail.just download from net and make changes and use

Related

PHP, email submission form

I have searched and searched for the answer and cannot find the answer to my particular problem. I am simply trying to send an email when a user fills out a contact page and clicks submit.
Below here is the code for the form.
<form method="post" action="contactRob.php">
<label for="name">First Name:</label>
<input type="text" name="name" id="name" />
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<label for="message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
And below here is the PHP I am using.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['lastName'];
$to = 'juliansilvestri92#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
What am I missing?
Currently this website is live on a hosted server domain.
When I click the submit button I am getting 'something went wrong' error throw that I placed.
Any help would be greatly appreciated.
Your names are first capital while results arent.
$message = $_POST['email'];
<input type="text" name="Email" id="Email" />
make both lowercase. Then echoing $body could help, see if the data is the same as you inputed, comment out the mailer part, see if the error comes from mail or from post DOM itself. If its mailer problem that's causing you the error, read about mailer a bit more, if that doesn't help.
and your from is incorrect.
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
Your from should look like this:
$from = "From: ".$_POST['email']."\r\n CC: ".$_POST['lastName'];
or something like that.
There is all ready similar topic about it, and there is a honest huge comment, you wont miss it.
PHP mail function doesn't complete sending of e-mail
Do what that comment is telling you to do, and you will most likely solve or understand the problem.
Good luck..
Most of the server not allow you to send email using just php mail function use php mailer function to send mail from any where local development server/ live web server.
https://github.com/PHPMailer/PHPMailer
also for default mail send you can use these params
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);

Changing the value of a Form Submit Button to say submitted after form submits

I have a contact form on my website. I'd like to change the text of the submit button to say "submitted" after the form has successfully submitted, and maybe even make it say "submitting" while the form is submitting. I am unsure of how to do this, i could do an onclick event that would change the text, but not the route i want to take as the message could fail to send and the button would still say submitted.
Here is my html for the form
<form method="post" action="contact.php">
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"></textarea><br>
<input id="submit" type="submit" name="submit" value="Let's Get In Touch">
</form>
and here is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Is there a way to do this with my existing php code? Thanks in advance for any help.
What you are going to need is something to pass the data to the php script, and return something/echo something back without leaving the page.
Take a look into AJAX. You will be able to exactly this.
Here's a link to one of the first posts on stackoverflow that showed up.
Here's a link to w3schools to give you a quick example/idea.
If you don't want to use AJAX and you're posting to page itself you can do the following
<form method="post" action=""> <!-- removed the PHP file name to post to itself -->
<input type="text" name="name" placeholder="Name"><br>
<input type="email" name="email" placeholder="Email"><br>
<textarea rows="8" cols="65" name="message"placeholder="Message"> </textarea><br>
<?php
if (isset($_POST['submit'])) {
echo '<input id="submit" type="button" name="submit" value="Submitted">'; //Changed type submit to button
} else {
echo '<input id="submit" type="submit" name="submit" value="Let\'s Get In Touch">';
}
?>
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Portfolio Website';
$to = 'kyle.a.binger#gmail.com';
$subject = 'Message From Personal Site';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>

PHP Mail Script hitting error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have a PHP mail script set up and keep hitting the error "Something went wrong, go back and try again!" all the form fields have been checked and all the names match etc so I am wondering if there is something wrong with my script?
<form method="post" action="contact.php" id="contactForm">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="name" />
<label for="email">Email</label>
<input type="text" id="email" name="email" class="email" />
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" class="phone" />
<label for="iam">I Am</label>
<select name="iam" class="iam" id="iam">
<option>a recruiter looking to recruit staff</option>
<option>a construction worker looking for work</option>
<option>requesting information</option>
</select>
<label for="message">Message</label>
<textarea name="message" id="message" class="message"></textarea>
<label for="captcha">What is 3+4?</label>
<input type="text" id="captcha" name="captcha" class="captcha" />
<input type="submit" name="submit" id="submit" class="submit" />
</form>
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$iam = $_POST['iam'];
$human = $_POST['captcha'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'sales#test.com';
$headers = "From: $email";
$subject = 'Tradeline Contact';
$body = "From: $name\n E-Mail: $email\n Phone Number:\n $phone I Am:\n $iam Message:\n $message";
if ($_POST['submit'] && $human == '7') {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
echo '<p>Your message has been sent!</p>';
header( 'Location: http://urlhere.com/thankyou.html' ) ;
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '7') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
Any help is appreciated.
You might want to change:
mail($to, $subject, $body, $headers, "-f " . $from)
to:
mail($to, $subject, $body, $headers."\r\n")
That way your mail headers will be in compliance.
Also, turn on error reporting. I happen to use error_reporting(7); right under the <?php line to turn on all common errors with the exception of catching undefined variables, and that will tell me if the mail function has problems.
Another thing you can do is check the mail server logs to see if mail is actually being sent.
I'm sure you already did this, but in case you haven't, make sure you use valid email addresses.

Form isn't sending

I'm staging a site for a client and I'm trying to create a form from scratch rather than use a plugin.
I'm not sure where I'm going wrong. The page keeps refreshing to the homepage and no email gets sent.
Could someone please point out in my code where I've gone wrong...
Thanks in advance!
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Test';
$to = 'email#example.com';
$subject = 'Hello';
if ($name == "" OR $email == "") {
echo "You must specify a value for name, email address, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
require_once("assets/inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
}
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
header("Location: http://natashamcdiarmid.com/clients/JLP/wp/contact/?status=thanks");
exit;
}
?>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks, I'll get back to your shortly!</p>
<?php } else ?>
<form method="post" action="contact">
<p><label>Name</label></p>
<input name="name" placeholder="Type Here">
<p><label>Email</label></p>
<input name="email" type="email" placeholder="Type Here">
<p><label>Message</label></p>
<textarea name="message" placeholder="Type Here"></textarea>
<p><label>*What is 2+2?</label></p>
<input name="human" placeholder="Type Here">
<p><input id="submit" name="submit" type="submit" value="Submit"></p>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
</form>
The form that you are posting to isn't much of a directory.
<form method="post" action="contact">
should it be contact.php?
this action should be the directory of your form handler
A major issue with WordPress that always gets me is that it uses some request variables with common names, and messing with them causes unpredictable errors. For instance, the name parameter is used to locate and display the current post or web page.
Try renaming your name field to something else, like your_name.
When I create forms for use in WordPress, typically I prefix every field with something custom, like acme_contact_name, acme_contact_email, etc. A little more typing, but safer.

Simple PHP Email form not working

Very simple email form code. Worked once, but won't work anymore, even at different addresses. That is to say the emails no longer show up, but I'm not erroring out eather.
<form action="/mail-us.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
And here is the PHP
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "blah#x-matic.net";
$subject = "X-Matic Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error sending email!");
?>
Note, I tested the email out with my gmail account (from gmail to email, instead of from form to email) and it worked.
Use SMTP mail when in localhost, and switch to mail() once the site is in a webserver (i.e your webhost)
At least that is what i do.
Unless you want to install Pegasus Mail on your computer, the mail() function will not work on your localhost computer, as your computer does not have a mail server installed.
Either that, or use SMTP.
if(isset($_POST['submit']))
{
$to = "xxx#google.com";
$subject = "Email from Sender; // quotation marks end ;
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
mail($to, $subject, $body);
}
else
{
echo "Mail sending Failure!";
}
?>
try this

Categories