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.
Related
This question already has answers here:
Php form failing
(1 answer)
Simple form not sending data via _POST [duplicate]
(4 answers)
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
My contact form in php doesnt send the message to the mail so i need to know whats the problem here
You will find the html form here with file name : index.php
and php form with name : mail.php
<form class="form" action="mail.php" method="post" name="contactform">
<input class="name" type="text" placeholder="Name" name="name">
<input class="email" type="email" placeholder="Email" name="email" >
<input class="phone" type="text" placeholder="Phone No:" name="phone">
<textarea class="message" id="message" cols="30" rows="10" placeholder="Message"name="message" ></textarea>
<input class="submit-btn" type="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit']) ) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'From: phone';
$to = 'modysaid26#gmail.com';
$subject = 'message';
$body = "From: $name\n E-Mail: $email\n Phone Number: $phone\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been submitted</p>';
} else {
echo '<p>Something went wrong, please try again!</p>';
}
}
}
?>
<input class="submit-btn" name='submit' type="submit" value="Submit">
You are missing to add the name to submit button so your case if (isset($_POST['submit']) ) { fails
you dont need to put name on the form tag remove the class either:
<form action="mail.php" method="post">
First yor submit button name is missing, please use a
<input class="submit-btn" type="submit" value="Submit" name="submit">
The second you email command ( mail ($to, $subject, $body, $from) ) has no right email header. Insead your $from
please define header with following parameters
$email_headers = "From: ".$from_name." <".$from_email.">\r\n".
"Reply-To: ".$reply_to."\r\n" ;
if ($cc) $email_headers.="Cc: ".$cc."\r\n";
if ($bcc) $email_headers.="Bcc: ".$bcc."\r\n";
$email_headers.="MIME-Version: 1.0" . "\r\n" .
"Content-type: text/html; charset=UTF-8" . "\r\n";
$email_body=$_POST['message'];
and then send it using
mail($to, $subject, $email_body, $email_headers);
And then your email shouldbe send properly.
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);
I am currently creating a contact form and with the help of thw world wide web I made a php background, which I also understand.
Unfortunately it looks al great to me, but I do get aan error, saying:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\jquery\mail.php on line 16 [line 16 is the place where the if statement with the mail and the attributes appaeat, see below]
I have honestly no clue what the error means, and my code is below, does anybody now what it means and how I could fix it? Thanks in advance!
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$city = $_POST['city'];
$message = $_POST['message'];
$from = 'From: timohoogie';
$to = 'ownemail#gmail.com';
$subject = 'Contact zoeken met whoduniit';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n City: $city\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>';
}
?>
<form method="post" action="mail.php">
<label for="Name">Naam:</label>
<input type="text" name="name" id="name" />
<label for="City">Stad:</label>
<input type="text" name="city" id="city" />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<label for="gevonden"> Hoe heeft u ons gevonden</label>
<input type="text" name="gevonden" id="gevonden" />
<br>
<br>
<label for="Message">Bericht:</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>
You need to have a SMTP server running on the machine that hosts this code with your current settings. Or else you need to change the SMTP settings in your php.ini file.
Read more about how to set this up here: http://www.quackit.com/php/tutorial/php_mail_configuration.cfm
If you want to set up your own SMTP server, I dont have any good tips on top of my head, but there are several free server softwares out there. Although, you would probably be better off using a public one, or even you ISP SMTP server.
mail is a PHP function, yes. But it must be installed on the server that is doing the actual sending. SMTP is a slightly different protocol that seems to be getting triggered because the simple mail function is failing (for it not being installed.)
You need to check with your service provider to see what mail function you should be using.
I just followed this tutorial on how to create a contact form with PHP. Now everything works fine but when I submit a form it returns a message on a new blank page. I want this to happen on the current page underneath the contact form.
This is my first time ever doing anything with PHP so I have no idea how I would do this. In the tutorial it is briefly mentioned that you can just put the script anywhere you would want the message to appear but it doesn't seem to work for me.
This is my HTML code:
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and this is my PHP code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$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 fields!!</p>';
}
}
?>
I've already found some answers but none of which make any sense to me. I also tried inspecting the example on the tutorial's site but, ofcourse I can't access the PHP.
Can anybody explain this to me?
If you want the message on the same page then you have to put the php code on the same page like this:
<form method="post" action="">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
<?php
if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message'])&&!empty($_POST['human']))// check if everything has been filled out before doing anything else
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$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 fields!!</p>';
}
}
}
?>
From what you appear to be asking you need to look into AJAX. You'll need to use a JavaScript library such as jQuery to make it easier. It will do exactly what you're looking for. Post the form data to your "contact.php" and the returned message (success/error) straight back to the page you are currently on. No redirect, no refresh.
There are different ways to go about this. AJAX is one of them, but since you are just starting out my opinion is to avoid that - learn the basics first. The code below is just that - the basics.
This method entails having the form and the processing on the same page. This is generally not the best method, but it is probably the simplest - and again, my opinion is that when you are starting out, simplicity is your friend.
contact.php
<?php
// First, check if the submit button has been pressed - no processing occurs unless the form has been submitted.
// This is necessary because your $_POST variables do not exist until the form is submitted.
if (isset($_POST['submit'])) {
// The button has been pressed, so now gather data and set variables
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
// Now, validate and process
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 {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
} else {
echo '<p>You need to fill in all fields!!</p>';
}
}
?>
<form method="post" action="contact.php">
<label>Name</label>
<input name="name" placeholder="John Doe">
<label>Email</label>
<input name="email" type="email" placeholder="john#doe.com">
<label>Message</label>
<textarea name="message" placeholder="Hello..."></textarea>
<label id="antispam">What is 2+2? (Anti-spam)</label>
<input id="antispambox" name="human" placeholder="4">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
Keep in mind that, while this method works, in the long run it is not the best approach. It is better to keep logic and markup separate. That would mean having at least two pages - one that displays the form, and another page that processes the form. Even better approaches will involve object oriented programming. However, I am a firm believer in the ready, fire, aim methodology... when you are trying to learn, get it to work first, worry about best practices and optimization later. As you run into bigger problems and the need for more complexity arises, you can make adjustments and figure out better ways to do things. Progress only comes from practice, trial, and error. Good luck.
I started a form in a PHP page and I think I have everything right were it belongs. But when I upload it to a server it doesn’t work. How to fix it?
Here is my code:
<section class="body">
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: TangledDemo';
$to = 'contact#tangledindesign.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>';
}
?>
<form method="post" action="new.php">
<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>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
</section>
so the error that i get is this whole line of code on my live site
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>';
}
?>
It appears that your web server does not support PHP. PHP runs on the web server (aka server side), and the client side (that is the web browser) should only get valid HTML.
It looks like your webserver is not interpreting the PHP. A quick check for this would be to view the source in your browser - if you see PHP code there, then the webserver did not run the PHP.
There are many possible reasons for PHP not being interpreted - the php extension not being handled, PHP not being installed, other web server configurations, etc.