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);
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.
Below is the code to my wordpress contact form, probably there is some mistake because when I enter my page url http://muzykablog.pl/ the url jumps automatically to http://muzykablog.pl/page-kontakt.php?msg_sent=true ...
This is the code inside my contact page (page-kontakt.php) :
<h4>Send Us Mail</h4><br/>
<?php
if ($_GET[msg_sent]=='true' ) {
echo '<div>Your message has been sent!</div>';
}elseif ($_GET[msg_sent]=='false') {
echo '<div>An error occurred sending your message.</div>';
}else{
?>
<form method="post" action="functions.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>
<?php } ?>
This is the code inside my functions page (functions.php) :
// KONTAKT - MESSAGE SENDING FUNCTIONS FOR page-kontakt.php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
There has to be some mistake here
It could be better to see the mail() function. But seeing the code it seems it will always return true or false so it will redirect to ?msg_sent=false or ?msg_sent=true. For some reason your page is always sending the form when loads. You must avoid that. Its rare too that the action of the form is functions.php. Are you trying to put the form in the home page?
I think there are more deep problems but it could be arranged (not very pro) with this in your functions.php
if($_POST['human']!=""){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: http://muzykablog.pl/';
$to = 'piterdeja#gmail.com';
$subject = 'Hello';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(mail($to, $subject, $body, $from)){
header('Location:page-kontakt.php?msg_sent=true');
}else{
header('Location:page-kontakt.php?msg_sent=false');
}
}
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.
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 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