Bootstrap Contact Form - How to send html email - php

I am using a bootstrap contact form. The form works fine but whatever I do I cannot get it to send html emails. I have added headers and css inlined to different parts of the script but it never sends the email with any html formatting. I am banging my head against the wall and would really appreciate any help. Where do I put the headers variable and where do I put the html?
Here is the php code.
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Mywebsite';
$to = 'me#email.com';
$subject = 'Contact Email ';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
$body = " $headers, From: $name\n E-Mail: $email\n Message: $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if content has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! </div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
And here is the HTML.
<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message from My website.
<br>
<br>
<br>
Login in to your account
<br>
<br>
</div>
<br>
<br>
</div>

replace mail($to, $subject, $body, $from); with mail($to, $subject, $body, $headers);

Related

Strange PHP parse error when creating a contact form [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
For some reason I keep getting an error within my backend code for my contact form
Here is my contact form
<div id = "form">
<form action ="contact2.php" method="post">
Hi Rebekah My Name Is
<br>
<input type="text" name="name">
<br>
My Email Is
<br>
<input type="text" name="email">
<br>
My Message Is
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>
<br>
<input type="submit" value="Submit">
<?php
include "contact2.php";
echo $result;
?>
</div>
Here is my backend PHP code
<?php
$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'example#yahoo.com';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
// Check if name has been entered
if (!$_POST['name']) {
$field_name = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$email = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$field_message = 'Please enter your message';
}
// If there are no errors, send the email
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
?>
When I run the code there is said to be an error in my php code on line 43 which is the last line but I can't seem to see what the problem is as this is only the closing tag for the php code, when I take the closing php tag out I still get the same message.
Here is the message that is displayed.
Parse error: syntax error, unexpected end of file in D:\contact2.php on line 43
I have taken out where the file is found from the above for safety reasons.
You are missing the closing tag of your if statement.
Add } before your closing tag ?> on the last line.
Your if (!$field_name && !$email && !$field_message) { is not closed.
Your last IF statement is lacking a }
if (!$field_name && !$email && !$field_message) {
if (mail ($_to, $subject, $body_message, $headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} // <-----

Any idea why my mail function doesn't work? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
HTML Code:
<form method="post" action="form.php">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" type="text" placeholder="Name">
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="email" name="email" type="email" placeholder="Email">
</div>
<div class="col-xs-12">
<textarea class="form-control" id="message" name="message" type="message" rows="5" placeholder="Message"></textarea>
</div>
<button type="button" id="submit">Submit</button>
<?php echo $result; ?>
</form>
PHP Code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Form';
$to = 'example#gmail.com';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You</div>';
} else {
$result='<div class="alert alert-danger">Please try again later</div>';
}
}
}
?>
So this is the code and I don't know why I still don't get mail when the form is submitted. I tried everything I know but is it still the same. Moreover, I am using a web hosting that supports send emails and PHP and more. And it still doesn't work.
There are multiple logical and syntactical issues there.
You need to validate $_POST before you assign it to any variable.
You are using $subject but you do not have any value in that variable.
You are mixing header values to the mail body
I tried to fix issues try this.
<?php
if (isset($_POST["submit"])) {
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Form';
$to = 'example#gmail.com';
$body = $message;
$subject="Demo message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n".'X-Mailer: PHP/' . phpversion();
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="alert alert-success">Thank You</div>';
} else {
$result='<div class="alert alert-danger">Please try again later</div>';
}
}
}
?>
For further details of refer this http://php.net/manual/en/function.mail.php

Add a subject field to PHP email form

I have an email form in PHP and I want to add a subject to the body of the email:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
This form submits an email with the subject "Message from Camino.bo", that's ok but I want to submit in the body of the email the value the user selects from a dropdown menu:
HTML:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option>Product A</option>
<option>Product B</option>
<option>Product C</option>
</select>
How can I do this?
PS: the subject field in my form is optional with no validation required.
Your variables are a bit mixed up. The 4th parameter of the mail() function is not 'From', it is 'Email Headers', which can be any number of header values in the correct format, each separated with a new line. The 'From' will just be one of those headers..
$to = 'you#youremailaddress.com';
$subject = 'My Email Subject';
$body = 'The body text of your email....';
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=iso-8859-1\r\n".
"From: YOU <your#email.com>\r\n";
// and others as required
mail($to, $subject, $body, $headers);
Add your POSTed options to the $body tag as required
I'm not sure if I'm missing something, but in your HTML, you want to add values to the remainder of your options:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option value="Product A">Product A</option>
<option value="Product B">Product B</option>
<option value="Product C">Product C</option>
</select>
Then your PHP could be as follows:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form <youremailaddress#domain.com>';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$select_subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message: $message\n Subject: $select_subject";
$headers = "From: " . $from;
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
Notice that I've added $select_subject and added it to your email $body variable. Also please replace 'youremailaddress#domain.com' with the FROM email address you wish to use.
Also, I don't know if you already handle this elsewhere in your code, but I think you should display a message to screen if either $errName, $errEmail or $errMessage evaluate to true. Right now, if either one of those is true (assuming the code you pasted here is complete) your end user would get nothing displayed on their screen upon form submission and they might be confused by what happened and try to resubmit.

Hide Email form after sumbission

I have a PHP email form embedded at the top of the HTML of my contact form page (index.php):
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
}
}
?>
HTML:
<form role="form" method="post" action="index.php">
...
</form>
and I wanted to hide the form only after successful submission. How can I do this?
In your CSS file;
.hide {
display:none;
}
(If unsure how to do this, this question should help)
Then inside your PHP if block;
$class = "";
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
$class = 'class="hide"';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
and finally, on index.php:
<form role="form" method="post" action="index.php" <?php echo $class ?>>
Now you can hide the entire form, with a little bit of CSS and a dynamic variable.
Edit:
If you wanted to avoid using an external or internal CSS file entirely, you could apply the CSS inline to your html element directly, like so;
$style = 'style = "display:none;"';
An alternative approach, if you're not using style sheets or any other CSS.
To hide form after submission and email success,give id to form(here its id="myForm", then use style to display:none as shown below)
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
echo "<style> #myForm{ display:none; } </style>";
} else {
$result='<div>Sorry there was an error sending your message.</div>';
echo "<style> #myForm{ display:block; } </style>";
}
}
}
?>
<form role="form" method="post" action="index.php" id="myForm">
</form>

Input form doesn't parse error and displays blank page on submit

EDIT: Changed PHP to recommended code, still can't get success/error to display on page.
I'm trying to get this contact page to work, but it seems to be breaking when I click the submit button. As soon as I click, it redirects me to /contact_form.php and shows a blank page. If all forms have been correctly filled in, the message does get sent. If any of them are wrong or not filled in, the same blank page is shown.
HTML:
<form method="post" action="contact_form.php">
<div class="input-group-lg">
<label for="name">Your Name<i>*</i></label>
<input name="name" type="text" class="form-control" placeholder="What should I call you?">
</div>
<div class="input-group-lg">
<label for="email">Email Address<i>*</i></label>
<input name="email" type="email" class="form-control" placeholder="Email Address">
</div>
<div class="input-group-lg">
<label for="phone">Phone Number</label>
<input name="phone" type="text" class="form-control" placeholder="Phone Number">
</div>
<div class="input-group-lg">
<label for="message">Message<i>*</i></label>
<textarea name="message" class="form-control" placeholder="What's on your mind?"></textarea>
</div>
<div class="input-group-lg">
<label for="human">What is 2 + 2? (Anti-spam)<i>*</i></label>
<input name="human" type="text" class="form-control" placeholder="2 + 2 = ?">
</div>
<div class="text-center">
<?php echo $result; ?>
</div>
<div class="text-center">
<input name="submit" type="submit" value="Send Message" class="btn btn-custom"></input>
</div>
</form>
PHP:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 4) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
}
else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
}
?>
Try this. I havnt tested it, but this should work like a charm. If you still get errors please post them.
if (isset ($_POST['name'])){ //RUN ONLY IF name IS SET
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$human = intval($_POST['human']);
// Error handling for missing data
if ((!$name) || (!$email) || (!$phone) || (!$message) || ($human !== 4)){
$result = '<div class="alert alert-danger"><i class="glyphicon glyphicon-warning-sign"></i> <strong>ERROR:</strong> You did not submit the following required information:</div>';
if(!$name){
$result .= '<div class="alert alert-danger"><strong>Please enter your name</strong></div>';
}else if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$result .= '<div class="alert alert-danger"><strong>Please enter a valid email address</strong></div>';
}else if(!$phone){
$result .= '<div class="alert alert-danger"><strong>Please enter a phone number</strong></div>';
}else if(!$message){
$result .= '<div class="alert alert-danger"><strong>Please enter your message</strong></div>';
}else if($human !== 4){
$result .= '<div class="alert alert-danger"><strong>Please correct your addition</strong></div>';
}
} else { // Error handling is ended, process form and send email
$from = 'From: example';
$to = 'example#gmail.com';
$subject = 'Message from X';
$body ="From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
mail($to, $subject, $body, $from);
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} // Close else after duplication checks
} else { // if the form is not posted with variables, place default empty variables so no warnings or errors show
$name = "";
$email = "";
$phone = "";
$message = "";
$human = "";
$result = "";
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
You have no print or echo statement in your PHP file ...
add echo $result to the end of your PHP file to see the results
Change your success code this. This will print your success or error msg on page reload.
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
print '<div class="alert alert-success">Thank You! I will be in touch</div>';
exit();
} else {
print '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
exit();
}
}
Determine if a variable is set:
if (!isset($errName) && !isset($errEmail) && !isset($errMessage) && !isset($errHuman)) {
if (mail ($to, $subject, $body, $from)) {
$result='í<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
if(isset($result)){
echo $result;}

Categories