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>';
}
Related
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
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>";
}
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.
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";
Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything?
Here is my form code:
<form method="POST" action="mailtest.php">
<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">
<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subjectf" placeholder="Type Here">
<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>
<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>
And here is the php script I have on the mailtest.php page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'rkoolman#bellsouth.net';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
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>You need to fill in all required fields!!</p>';
}
}
?>
You should always first check whether the form was submitted so
remove if ($_POST['submit']) and put it on the top of the script..
also you should check whther the $_POST["submit"] was set.. the script should look like this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'test#test.com';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($name != '' && $email != '' && $message != '') {
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>You need to fill in all required fields!!</p>';
}
}
Set your submit input value to submit
<input class="submit" type="submit" name="submit" value="submit">