how can i send php mail with html contact form - php

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);
}
?>

Related

adding a success message to a html form with php

I´m tring to add a success message to my form after you click submit, but i dont know how, i have tried different code from different websites, but they dont work with my code. What line do i need to add to this code please?
<form action="contact.php" method="post" id="contact">
<h1>Contact-me!</h1>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="name#example.com">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea name="message" class="form-control" id="message" rows="3"></textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" value="SEND MESSAGE">Submit</button>
<div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>

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';
}
?>

Bootstrap 4 form not sedning any messages

My bootstrap 4 form is not sending any messages, I've uploaded the index.php and contact.php files on the server and I'm not having any response. Would you be able to help me find the problem?
Landing page with simple form
<form id="#contacts" method="POST" class="form" role="form">
<div class="row">
<div class="col-md-6 form-group">
<input class="form-control" id="name" name="name"
placeholder="Podaj swoje Imię i nazwisko" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="website" name="website"
placeholder="Adres strony www" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="telephone" name="telephone"
placeholder="Podaj swój numer telefonu" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="email" name="email"
placeholder="Podaj swój email" type="email" required/>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Twoja wiadomość"
rows="4"></textarea>
<br>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn btn-primary" type="submit">Wyślij</button>
</div>
</div>
</form>
contact.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$website = $_POST['website'];
$telephone = $_POST['telephone'];
$email_from = 'SEO';
$email_subject = 'Wiadomość kontaktowa z... '
$email_body = "Name: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
"Website: $website.\n";
"Telephone: $telephone.\n";
$to = "biuro#of.pl";
$headers = "From: $email_from r\n";
$headers .= "Reply-To: $email r\n";
mail($to,$email-$email_subject,$email_body,$headers);
header("location: index.php");
?>
Your form is missing Action
<form id="#contacts" method="POST" class="form" role="form" action="contact.php">

Html + PHP contact form (Page Not found)

I have created a simple Html page index.html where the user can submit a message. An additional PHP file email_form.php is created to capture the form submissions and send the form contents to my email address.
index.html:
<form name="contactform" method="POST" action="email_form.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
email_form.php:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
On the website, when I try to submit a message, it redirects me to a link looking like this: mysite.com/email_form.php with an error saying "page not found". I don't know much about PHP and I can't tell what I'm doing wrong.
First of all you should have a closing form tag </form>
Then:
Paste the php code to the top of your HTML file and
Change the file's extension to .php
Change the form's action to index.php
Your index.php should look something like this:
<?php
if(isset($_POST['submit'])) {
$email_to = "info#mysite.com";
$email_subject = "Email subject";
$prenom = $_POST['prenom'];
$nom = $_POST['nom'];
$age = $_POST['age'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Prénom: ".clean_string($prenom)."\n";
$email_message .= "Nom: ".clean_string($nom)."\n";
$email_message .= "Age: ".clean_string($age)."\n";
$email_message .= "Telephone: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = "From: ".$email."\n\n";
mail($email_to, $email_subject, $email_message, $headers);
header("Location: index.html");
}
?>
<form name="contactform" method="POST" action="index.php">
<div class="form-group">
<div class="form-row">
<div class="col" id="prenom">
<input type="text" class="form-control" placeholder="Prénom" required="True" name="prenom">
</div>
<div class="col" id="nom">
<input type="text" class="form-control" placeholder="Nom" required="True" name="nom">
</div>
</div>
<div class="col" id="age">
<input type="number" class="form-control" placeholder="Age" required="True" name="age">
</div>
<div class="form-row">
<div class="col" id="tel">
<input type="tel" class="form-control" placeholder="Téléphone" name="tel">
</div>
<div class="col" id="mail">
<input type="email" class="form-control" placeholder="Email" name="email">
</div>
</div>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" placeholder="Your message" name="message"></textarea>
<div class="row">
<div class="col-xs-6" id="button">
<button type="submit" name="submit" class="btn btn-primary">Send</button>
</div>
</div>
</div>
</form>
I have resolved the problem by changing headerin email_form.php. Now it looks like this:
header("Location: https://example.com");
And it works fine.

PHP mail() function sends empty variables

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>";

Categories