I'm using a template for a contact form for my website. The problem is, when I test it on my server I cannot send a message. It will only give me my error message and not send. Did something happen when I translated the original send text to German?
I've tried debugging the vars. I've also tried to echo some string inputs.
This is the html code:
<form id="contact-form" action="mail.php" method="post">
<div class="row">
<div class="col-md-6 form-group">
<label class="sr-only">Name</label>
<input type="text" class="form-control input-lg" name="name" placeholder="Name" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-6 form-group">
<label class="sr-only">Email</label>
<input type="email" class="form-control input-lg" name="email" placeholder="Email" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<label class="sr-only">Betreff</label>
<input type="text" class="form-control input-lg" name="subject" placeholder="Betreff" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<textarea class="form-control input-lg" rows="7" name="message" placeholder="Nachricht"></textarea>
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-lg btn-round btn-dark">Senden</button>
</div>
</div>
</form>
PHP:
<?php
// POST GET.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Form Felder GET.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$subject = trim($_POST["betreff"]);
$message = trim($_POST["nachricht"]);
// Check ob Daten an den mailer.
if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// 400 (bad request) und raus.
http_response_code(400);
echo "Bitte füllen Sie alle Felder aus.";
exit;
}
// Empfaenger.
$recipient = "info#test.de";
// Betreff.
$subject = "Neue Anfrage von $name";
// Inhalt.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Betreff: $subject\n\n";
$email_content .= "Nachricht:\n$message\n";
// Header.
$email_headers = "Von: $name <$email>";
// Senden.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// 200 (okay).
http_response_code(200);
echo "Vielen Dank! Deine Nachricht wurde versendet.";
} else {
// 500 (internal server error).
http_response_code(500);
echo "Oops! Hier ist ein Fehler passiert, deine Nachricht konnte nicht gesendet werden.";
}
} else {
// 403 (forbidden).
http_response_code(403);
echo "Es gibt Probleme mit deiner Anfrage, bitte versuche es ernuet.";
}
?>
Your input name for subject is in English in your form, but in your php file is in German
Choosing the same lang for all your code can to avoid confussions
Change name attributes to german to make it work
<input type="text" class="form-control input-lg" name="betreff" placeholder="Betreff" >
<textarea class="form-control input-lg" rows="7" name="nachricht" placeholder="Nachricht">
</textarea>
Related
I am having issues with my form built is HTML and PHP. It is returning a white page after I clicked to submit. The website is online already http://diegodiasp.com/
The html is the following:
<form class="bg-light p-4 p-md-5 contact-form" action="./contactform.php" method="POST" >
<div class="form-group">
<input class="form-control" name="name" placeholder="Your Name">
</div>
<div class="form-group">
<input class="form-control" name="mail" placeholder="Your Email">
</div>
<div class="form-group">
<textarea class="form-control" name="message" cols="30" rows="7" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" value="Send Message" class="btn btn-primary py-3 px-5">
</div>
</form>
And the PHP is as bellow:
<?php
if(isset($_POST['email']) && !empty($_POST['email'])){
$name = addslashes($_POST['name']);
$email = addslashes($_POST['mail']);
$message = addslashes($_POST['message']);
$to = "info#diegodiasp.com";
$subject = "Contact - Diego Dias Front End";
$body = "Name: ".$name."\r\n".
"Email: ".$email."\r\n".
"Message: ".$message;
$header = "From:info#diegodiasp.com"."\r\n"."Reply-To:".$email."\r\n"."X=Mailer:PHP/".phpversion();
if(mail($to,$subject,$body,$header)){
echo("Email succesfully sent!");
}else{
echo("Email not sent!");
}
}
?>
The textbox for the email address is called mail.
You are checking if 'email' isset and not 'mail'.
Correct your php to the following:
if(isset($_POST['mail']) && !empty($_POST['mail'])){
I am buidling a php contact form with ajax validation. While I submit the contact form I get error message "spammer" which is not supposed to.
Below is my code html & php code.
Index.php
<form id="contactform" method="post" action="mailer.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="username" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Enter your phone number">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" rows="4" placeholder="Enter your email message"></textarea>
</div>
<br>
<div class="g-recaptcha" data-sitekey="6Lct9zUUAAAAABpj9vwKX9B7x_AH8s9gwJzFW1sB"></div>
<br>
<div id="formresult">
</div>
<button type="submit" name="submit" class="btn btn-info">Submit</button>
</form>
Mailer.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["username"]);
$phone = trim($_POST["phone"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
//Validate the data
if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
http_response_code(400);
echo "<span class='glyphicon glyphicon-remove' aria-hidden='true'></span> <strong>Please fill all the form inputs and check the captcha to submit.</strong>";
exit;
}
//recipient email address.
$recipient = "abc#abc.com";
$subject = "New message from $name";
//email content.
$email_content = "Name: $name\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers
$email_headers = "From: $name <$email>";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRATE_KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);
if($decoded_response['success'] == true){
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo "<span class='glyphicon glyphicon-ok' aria-hidden='true'></span> <strong>Thank You! Your message has been sent.</strong>";
} else {
http_response_code(500);
echo "Your message cannot be sent";
}
} else {
http_response_code(400);
echo 'spammer!';
}
}
?>
When I submit the form I get a error message 'spammer' and even mail does not get through.
I know there is some mistake in logic but i cannot figure it out.
Anyone please help me in this. Thank you.
The line of code:
if($decoded_response['success'] == true){
Must be evaluating to false, and executing the else part of that statement. Displaying the spammer message.
Check the decoded contents returned from the recaptcha url, that you assign to the $decoded_response variable.
print_r($decoded_response); // should show the whole contents
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
Hi I have a Contact form and a PHP file that handles the email after the form is successfully filled out. For some reason I am not receiving any emails from this form. Was just curious if my code was correct.
HTML FORM:
<form method="post" role="form" name="sentMessage" id="contactForm" action="/public_html/mail/contact_me.php" novalidate>
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" name="name" required >
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" name="email" required >
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" name="phone" required >
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" name="message" required ></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" class="btn btn-xl" >Send Message</button>
</div>
</form>
PHP CODE:
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'will#blacart.com';
$email_subject = "Website Contact: $name";
$email_body = "You have received a contact request from tandemmetal.com.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply#tandemmetals.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
You can try to use \r\n instead of \n.
$headers = "From: noreply#tandemmetals.com\r\n";
$headers .= "Reply-To: $email_address\r\n";
According to http://php.net/manual/ru/function.mail.php
(in case, if your server on Windows).
I have been trying to add php to a bootstrap form via the following tutorial: https://bootstrapbay.com/blog/working-bootstrap-contact-form/. But nothing happens when I click the submit button - I just go back/stay to the same page with an empty form. I don't receive an email nor any error actions when I don't fill out some fields either. Below my code. The name of the document is werkwijze.php. Does someone have an idea what I'm doing wrong? Thanks a lot in advance!
<?php
if (isset($_POST['submit'])){
$voornaam = $_POST['voornaam'];
$familienaam = $_POST['familienaam'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$vraag = $_POST['vraag'];
$from = 'info#tbep.be';
$to = 'eefje.vanhemelryck#gmail.com';
$subject = 'Vraag van op uw website www.tbep.be';
$body = "From: $voornaam $familienaam\n E-Mail: $email\n Telefoon: $tel\n Vraag: $vraag";
// Check if name has been entered
if (!$_POST['voornaam']) {
$errVoornaam = 'Gelieve uw voornaam op te geven';
}
if (!$_POST['familienaam']) {
$errFamilienaam = 'Gelieve uw familienaam op te geven';
}
// Check if email en phone has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Gelieve uw emailadres op te geven';
}
if (!$_POST['tel']) {
$errTel = 'Gelieve uw telefoonnummer op te geven';
}
//Check if message has been entered
if (!$_POST['vraag']) {
$errVraag = 'Gelieve uw vraag te stellen';
}
// If there are no errors, send the email
if (!$errVoornaam && !$errFamilienaam && !$errEmail && !$errTel && !$errVraag) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Hartelijk dank. Ik neem zo snel mogelijk contact met u op!</div>';
} else {
$result='<div class="alert alert-danger">Sorry, er was een probleem met het versturen van dit formulier. Alternatief kan u ons een email sturen op info#tbep.be</div>';
}
}
}
?>
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="text/plain">
<div class="form-group">
<label class="control-label col-sm-3" for="voornaam">Voornaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="voornaam" name="voornaam" placeholder="Voornaam">
<?php echo "<p class='text-danger'>$errVoornaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="familienaam">Familienaam:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="familienaam" name="familienaam" placeholder="Familienaam">
<?php echo "<p class='text-danger'>$errFamilienaam</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">Email:</label>
<div class="col-sm-9">
<input type="email" class="form-control" id="email" name="email" placeholder="Email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="telefoon">Telefoonnummer:</label>
<div class="col-sm-9">
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefoonnummer">
<?php echo "<p class='text-danger'>$errTel</p>";?>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="vraag">Uw vraag:</label>
<div class="col-sm-9">
<textarea class="form-control" rows="5" id="vraag" name="vraag" placeholder="Uw vraag"></textarea>
<?php echo "<p class='text-danger'>$errVraag</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" id="submit" name="submit" value="Send" class="btn btn-default">Versturen</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
<?php echo $result; ?>
</div>
</div>
</form>
Please Remove enctype="text/plain" from your form.
<form class="form-horizontal" role="form" action="werkwijze.php" method="post">
Or use
<form class="form-horizontal" role="form" action="werkwijze.php" method="post" enctype="multipart/form-data">
Hey StackOverflow Community, i have a problem with my Contact Form.
I used this PHP Script:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Kontaktformular';
$to = 'email#mail.com';
$subject = 'Buchungsanfrage - $name';
$body ="From: $name\n: $phone\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Bitte geben Sie den Namen ein.';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Bitte geben Sie eine gültige E-Mail Adresse ein.';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Bitte hinterlassen Sie mir eine Nachricht.';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Ihre Anti-Span Antwort war falsch.';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Danke! Ich werde mich schnellstmöglich bei Ihnen melden.</div>';
} else {
$result='<div class="alert alert-danger">Entschuldigung, beim versenden Ihrer Nachricht ist etwas schief gelaufen. Bitte versuchen Sie es nochmal.</div>';
}
}
}
?>
And this HTML for my Form:
<div class="block block-primary-head no-pad">
<h3><i class="fa fa-pencil"></i> Buchungsanfrage</h3>
<div class="block-content">
<form role="form" method="post" action="fotografie.php">
<div class="form-group">
<label>Name*</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Vor- und Nachname" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label>Telefonnummer</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Ihre Telefonnummer" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
</div>
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label>Nachricht</label>
<textarea class="form-control" id="message" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<label>2 + 3 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Ihre Antwort">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<?php echo $result; ?>
</div>
<button type="submit" id="submit" name="submit" value="send" class="btn btn-primary">Absenden</button>
</form>
</div>
But when i go on my side now, i got the following error:
Notice: Undefined index: submit in ..\htdocs\projects\bootstrap-themes\001\contact\form-fotografie.php on line 2
Line 2 is following code:
if ($_POST["submit"]) {
What could be the problem?
Thanks for your help :-)
It looks like you are trying to check if the form is submitted. You need to use the isset function for this as below
Line 2:
if ($_POST["submit"]) {
Should read:
if (isset($_POST["submit"])) {
EDIT:
You should always check if the variable is set before using it so to correct your html you need to change the following:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
To:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php if(isset($_POST['email'])) { echo htmlspecialchars($_POST['email']); } ?>">
<?php if(isset($errEmail) && ($errEmail) != "") { echo "<p class='text-danger'>$errEmail</p>"; } ?>
</div>
You need to make this change for all of the form-groups with the relevant variable names, let me know how you get on