PHP mail() function sends empty variables - php

For some reason name, email and message appear empty when I receive an email.
HTML
<form method="post" action="send.php" class="form-horizontal">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-4">
<input id="name" name="name" type="text" class="form-control input-md" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-4">
<input id="email" name="email" type="email" class="form-control input-md" required="">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="message">Message</label>
<div class="col-md-4">
<textarea class="form-control" id="message" name="message"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" type="submit" value="Submit" class="btn btn-outline-inverse btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
PHP Code Simplified example
<?php
$emailto = "example#example.com";
$subject = "Example subject";
$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$headers = "From: example#example.com\r\n";
$headers .= "Reply-To: example#example.com\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";
$success = mail($emailto, $subject, $body, $headers);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
For some reason variables $name, $email and $message appear empty when I receive an email from this contact form/mail function. The rest is shown correctly.

You also need to close the " in the $body variable.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Poruka: </strong> {$message} </p>";

The problem was in my htaccess file and not in the code, I had some lines which were removing php and html extensions for estethics, so its all works fine now.

Try this.
$body = "<p>You have received a new message:</p>
<p><strong>Ime: </strong> ".$name." </p>
<p><strong>Email Address: </strong> ".$email." </p>
<p><strong>Poruka: </strong> ".$message." </p>";

Related

405 Method Not Allowed - Contact Form HTML + form.php

I am trying to enable a simple contact form, however, every time on the button submit, I get redirected to /form.php with the error 405 Method Not Allowed in the console.
Here is the form in index.html
<div class="container text-center pt-5 pb-5" id="contact">
<h1 class="word">Contact Us</h1>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<form action="form.php" method="POST">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control text-white" id="name" name="name" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control text-white" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control text-white" id="message" name="message" rows="3" placeholder="Enter your message"></textarea>
</div>
<button type="submit" class="btn btn-primary mt-3 btn-lg mt-4">Send Message</button>
</form>
</div>
</div>
</div>
</div>
And here is my form.php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the form data
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Set the recipient email address
$to = "hello#doctrina.ai";
// Set the email subject
$subject = "Message from Contact Form";
// Build the email content
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers
$email_headers = "From: $name <$email>";
// Send the email
mail($to, $subject, $email_content, $email_headers);
// Redirect to the thank you page
header('Location: thanks.html');
}
?>
I have thanks.html page as well. I am using Vercel for deployment. Thank you in advance for your help!

PHP: Contact us form does not echo success text

I have two html forms that both send emails successfully using PHP. However, one form echo successful submission text, but the other one does not.
Here is the form that is part of the index.html page, which does not echo any text after submission. Please note, the email still sends:
<form class="booking-form" id="myForm" action="#">
<div class="row">
<div class="col-lg-12 d-flex flex-column">
<input name="name" placeholder="Your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Your Name'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="subject" placeholder="subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'subject'" class="form-control mt-20" required="" type="text">
</div>
<div class="col-lg-6 d-flex flex-column">
<input name="email" placeholder="Email" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Email'" class="form-control mt-20" required="" type="email">
</div>
<div class="col-lg-12 flex-column">
<textarea rows="5" class="form-control mt-20" name="message" placeholder="Messege" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Messege'" required=""></textarea>
</div>
<div class="col-lg-12 d-flex justify-content-end send-btn">
<button type="submit" class="genric-btn primary mt-20 text-uppercase ">Send</button>
</div>
</div>
</form>
The following is the PHP mailer code:
<?php
$to = 'myEmail#gmail.com';
$firstname = $_POST["name"];
$email= $_POST["email"];
$text= $_POST["message"];
$subject= $_POST["subject"];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: Email coming from Brave Towing " . $email . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = "
<html>
<head>
</head>
<body style=\"background-color:#fafafa;\">
<div style=\"padding:20px;\">
Customer Email: <span style=\"color:#888\">$email</span>
<br>
Contact Subject: <span style=\"color:#888\">$subject</span>
<br>
Contact Message: <div style=\"color:#888\">$text</div>
</div>
</body>
</html>
";
if (#mail($to, $email, $message, $headers))
{
echo '<h1>The message has been sent</h1>';
}else{
echo 'failed';
}
?>

how can i send php mail with html contact form

I am currently trying to send an HTML contact form or the contents of it using php mail () function. Just hang up and ask for help.
How can I e.g. Your entered text and the email address specified in the contact form via PHP?
So my code looks currently and it works .. Only I do not know how I the data entered and send it with ..
HTML CODE:
<form class="" action="senden.php" method="post">
<div class="">
<label>Name</label>
<input type="text" name="von" placeholder="Bitte geben Sie Ihren Namen ein" value="">
<label>E-Mail Adresse</label>
<input type="text" name="email" placeholder="Bitte geben Sie Ihre Email-Adresse ein" value="">
<label>Telefonnummer</label>
<input type="tel" name="tel" placeholder="Bitte geben Sie Ihre Telefonnummer ein" value="">
</div>
<div class="">
<label>Nachricht</label>
<textarea placeholder="Bitte geben Sie Ihre Nachricht ein" name="nachricht" rows="8" cols="40"></textarea>
<button type="submit" name="submit" value="">Nachricht Abschicken</button>
</div>
</form>
`
PHP CODE:
<?php
$empfaenger = "mymail#gmail.com";
$betreff = "Mail Function";
$from = "From: Max Reimers <absender#domain.de>";
$text = "Email Email Email Email Text Text Text";
mail($empfaenger, $betreff, $text, $from);
?>
Here is Code:
<form class="form-horizontal" action="" id="contact-form" method="post">
<fieldset>
<legend class="text-center colheader">Email Us</legend>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="name" name="name" type="text" placeholder="Full Name" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="email" name="email" type="text" placeholder="Email Address" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="phone" name="phonenumber" type="text" placeholder="Phone" class="form-control" required>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input id="subject" name="subject" type="text" placeholder="Subject" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<textarea class="form-control" id="message" name="messag" placeholder="Enter your massage for us here. We will get back to you within 2 business days." rows="7" required></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-12 text-center">
<button type="submit" name="submit" class="btn btn-primary btn-lg">Submit</button>
</div>
</div>
</fieldset>
</form>
<?php
if(isset($_POST['submit'])) {
$to = 'ourmail#example.com';
$from = $_POST['email'];
$subject ='Adlivetech Contact Form';
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phonenumber'];
$subject = $_POST['subject'];
$messag = $_POST['messag'];
$message = '<html><body>';
$message .='<table>';
$message .='<tr><td>Name:</td><td>'. $name .'</td></tr>';
$message .='<tr><td>Email:</td><td>'. $email .'</td></tr>';
$message .='<tr><td>Phone:</td><td>'. $phone .'</td></tr>';
$message .='<tr><td>Subject:</td><td>'. $subject .'</td></tr>';
$message .='<tr><td>Message:</td><td>'. $messag .'</td></tr>';
$message .='</table>';
$message .='</body></html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "To: {$to}\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$to}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
mail($from, $subject, $message, $headers);
}
?>

PHP Contact form redirects

So I have a from that basically when it submits it runs through what i have on "contact-process.php" and when clicking on submit it will go to that page and then show the success or error, how do I make the submit show the success or error message on the same page and clear the form to show it's sent. here's the code I have on my web page.
<form class="form-horizontal" method="post" action="contact-process.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
And then this is the code I have on "contact-process.php"
<?php
if(isset($_POST['submit'])){
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
$errHuman = 'Your anti-spam is incorrect';
}
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
?>
Put the html to your PHP script as following:
<form class="form-horizontal" method="post" action="#">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#gmail.com">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject2" name="subject2" placeholder="Logo Design - Request">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="Please explain your request in detail. Provide screenshots & links."></textarea>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">5 x 2 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input type="submit" class="btn-primary btn" id="submit" name="submit" value="Send">
</div>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if($_POST["name"] != "" AND $_POST["email"] != "" AND $_POST["subject2"] != "" AND $_POST["message"] != "") {
$name = $_POST["name"];
$email = $_POST["email"];
$subject2 = $_POST["subject2"];
$message = $_POST["message"];
$human = intval($_POST['human']);
$EmailTo = "example#outlook.com";
$Subject = "Message Received";
if ($human !== 10) {
echo "<div class='alert alert-danger'>Your anti-spam is incorrect</div>";
}else{
// prepare email body text
$Body = "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n\n\n";
$Body .= "Subject: ";
$Body .= $subject2;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "<div class='alert alert-success'>Thank You! I will be in touch</div>";
}else{
echo "<div class='alert alert-danger'>Sorry there was an error sending your message. Please try again later.</div>";
}
}
}else{
echo "<div class='alert alert-success'>Data incomplate</div>";
}
}else{
// do nothing
}
?>
EDIT
action will be:
action="<?php echo $_SERVER['PHP_SELF']; ?>"
OR
action="#"
OR
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
Seek ye first ... then asks
You need to know a little Ajax is a technology that allows you to communicate with php javascript / JSON
Example:
https://scotch.io/tutorials/submitting-ajax-forms-with-jquery
Make a script with alert function then inside the script put location.reload in the success part.

E-mail sending form php/html

I,m trying to make a form send an e-mail but im getting page not found error.
This is the form itself
<form action="sendmail.php" method="post">
<div>
<div class="row half">
<div class="6u">
<input type="text" class="text" name="name" placeholder="Name" />
</div>
<div class="6u">
<input type="text" class="text" name="email" placeholder="Email" />
</div>
</div>
<div class="row half">
<div class="12u">
<input type="text" class="text" name="subject" placeholder="Subject" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input type="submit" class="button" value="Send Message" />
</div>
</div>
</div>
</form>
And this is the PHP file named sendmail.php
<?php
$to = "info#aroundgalaxy.pt";
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
//$site = $_REQUEST['site'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
$headers = "noreply#aroundgalaxy.pt";
$body = "From: $name \n\n Email: $email \n\n Message: $message";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
else
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
?>
Am i missing something?
Thanks
The code should work. I dont think there is a problem with the code. Just make sure both the form page & sendmail.php are on the same directory

Categories