Error in creating contact form with php - php

I created a simple contact form as follows.
<form method="post" action="mail_receive.php">
<label>Name *</label>
<input name="name" placeholder="Type Here">
<label>Email *</label>
<input name="email" type="email" placeholder="Type Here">
<label>Phone No *</label>
<input name="phone" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="">
And php file called mail_receive.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$from = 'From: Someone';
$to = 'admin#gmail.com';
$subject = 'Ticket Ordering';
$body = "From: $name\n E-Mail: $email\n Phone:\n $phone";
if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $phone != '') {
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 {
echo '<p>Please fill all required fields !!</p>';
}
}?>
}
And i uploaded to my hosting.
Although I can fill up the form and submit, I can't receive email to my inbox.
Is there any wrong?

With mail, it's meant to be
mail($to, $subject, $message, $headers);
Try something like this:
$to = "youremailaddress#whatever.com";
$subject = "Your tickets yo";
$headers = "From: someone#someone.com\r\n";
$headers .= "Reply-To: someone#someone.com\r\n";
$headers .= "MIME=VERSION:1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n;"
$message = "Nice tickets bro.";
mail($to, $subject, $message, $headers);
And please I hope it's just for example purposes, but please don't actually be sending it to admin#gmail.com because obviously (unless you ARE actually admin#gmail.com, you wont recieve the e-mail.

Related

php form works on one site but not another

I've been using the same contact form for quite a while:
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['names'];
$email = $_POST['email'];
$message = $_POST['message'];
$robot = $_POST['robot'];
$from = 'online#domain.co.uk';
$to = 'info#domain.co.uk';
$subject = 'Online Enquiry';
$headers = "From: DOMAIN <no-reply#domain.co.uk> \r\n";
$headers .= 'Reply-To:'. $email . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion(); // Sender's Email
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
} ?>
<?php if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $message !='') {
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}
} else if ($_POST['submit'] && $robot == 'yes') {
echo '<p class="warning"><strong>Looks like you are spam!</strong></p>';
}
} else {
echo '<p class="warning"><strong>Please complete all fields.</strong></p>';
}
}
?>
<form id="contact" method="post" action="#contact">
<label>Name</label>
<input name="names" type="text" placeholder="NAME">
<label>Email</label>
<input name="email" type="email" placeholder="EMAIL">
<label>Message</label>
<textarea rows="10" name="message" placeholder="MESSAGE"></textarea>
<input id="capture" name="robot" type="checkbox" value="yes">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
It works absolutely fine on other sites but doesn't work on one domain? submit simply returns the error "Something went wrong!". the site is on the same cloud server as many of the other sites that work fine. It's driving me mad because I cant see an error.
Turning debugging on I get the error:
Notice: Undefined index: robot in /form-contact.php on line 6
Ive tried removing the robot check all together and it still doesn't work, but generates no error when debugging?
Can anyone offer any advice?
When your checkbox is not checked - it will not be sent.
Replace
$robot = $_POST['robot'];
to
$robot = isset($_POST['robot']) ? 'yes' : 'no';
Your Condition Matches the case. Try checking the value of $robot on submit.
<input id="capture" name="robot" type="checkbox" value="yes">
if ($robot != 'yes') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p class="approved"><strong>Your message has been submitted</strong></p>';
} else {
echo '<p class="warning"><strong>Something went wrong!</strong></p>';
}

PHP mail reply to sender instead of server email

Can anyone point out where am I getting this form wrong? Instead of being able to reply to the "email" from the sender I get the server email.
Here is the php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: business.com';
$to = 'anyone#gmail.com';
$subject = 'A new Message from your website business.com';
$human = $_POST['human'];
$headers = 'From: info#business.com' . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($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>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
and here is the html
<form method="post" action="contact.php" target="contactIframe" name="contact">
<label>Name</label>
<input name="name" placeholder="Type Here">
<label>Email</label>
<input name="email" type="email" placeholder="Type Here">
<label>Message</label>
<textarea name="message" placeholder="Type Here"></textarea></br>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here"></br></br>
<input id="submit" name="submit" type="submit" value="Submit">
<input type="button" name="reset_form" value="Clear" onclick="this.form.reset();">
</form>
Thanks everyone
You mail function is does not have $headers as additional headers. The code should be...
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
}
}
ref : PHP Mail Function

Processing a form on the same page

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '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'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

Contact form. How do I get the name along with the email?

It's my first time trying to make a contactform. And I've got a few problems
It's works, I get the email, but I don't get the name the name field with me in the email.
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<h3>Name</h3>
<input type="text" name="name">
<h3>Email Address</h3>
<input type="text" name="email">
<h3>Message</h3>
<textarea name="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form">
</form>
PHP:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $name, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
$name is my problem. I've I have it in, the email comes from hostmaster#domane.com, If I delete it, everything works fine. But I wan't the name to be sent to me. How?
Or should I do it completely different?
Also, if you leave all the fields blank, the "user" doesn't get any error message, and a blank email is sent to me.
Hope you can help me. :)
Michael Berkowski is correct. What you'll need to do is add the name to your message's body (not in the sense of the input name= attribute, rather the body of the email).
Something like this:
<?php
$to = "name#domane.com";
$subject = "Contact Us";
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$headers = "From: $email";
$body = "Name: $name\r\n";
$body .= "Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "One of the field are not filled as requirred"; }
?>
Revised:
HTML:
<form method="post" name="contactform" action="scripts/contact.php">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
<label for="email">Email Address</label>
<input type="text" name="email" id="email" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<br/><input class="submit" type="submit" value="Send Form" />
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'] ;
$body = "Name: $name\r\n";
$body .= "Message: $message";
$to = "name#domane.com";
$from = "automailer#mydomainname.com (Website Automailer)";
$subject = "Contact Us";
$headers = "From: $from\r\n" .
"Reply-To: $email ($name)";
$sent = mail($to, $subject, $body, $headers) ;
if($sent) { echo "Your mail was sent successfully"; }
else { echo "One of the field are not filled as requirred"; }
?>
You should read the mail function documentation on php.net.
Have a look at the function signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Now you're placing $name as the "$additional_headers" argument. You should pass $name and any extra relevant data in a $message argument instead.
Having that said, here's the correct code to send a message:
$sent = mail($to, $subject, "A message from $name: $message", $headers);
You should read more about how email messages are constructed. Instead of just putting a user defined message in there you probably want to specify some email headers, containing a more beautiful FROM: and the like...

Categories