Copy link to clipboard
Copied
Hi,
I have recently added a contact form to my website, but it doesnt seem to send the message to my email when i click the 'submit' button. Can someone please have a look at it and tell me what the problem is? Thank you ! I would greatly appreciate it!
This is the code in the html file:
<link href="form.css" rel="stylesheet" type="text/css" />
<div class="container">
<form id="contact" action="contact.php"" method="post">
<h2 align="center" class="style11"><u>CONTACT FOR MORE INFORMATION </u></h2>
<p align="center"> </p>
<div class="contactFrm">
<div align="center">
<?php if(!empty($statusMsg)){ ?>
</div>
<p align="center" class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<div align="center">
<?php } ?>
</div>
<form action="" method="post">
<h4 align="center" class="style8">Name</h4>
<div align="center">
<input type="text" name="name" placeholder="Your Name" required="">
</div>
<h4 align="center" class="style8">Email </h4>
<div align="center">
<input type="email" name="email" placeholder="email@example.com" required="">
</div>
<h4 align="center" class="style8">Subject</h4>
<div align="center">
<input type="text" name="subject" placeholder="Write subject" required="">
</div>
<h4 align="center" class="style8">Message</h4>
<div align="center">
<textarea name="message" placeholder="Write your message here" required=""> </textarea>
<input type="submit" name="submit" value="Submit">
</div>
<div class="clear"> </div>
</form>
</div>
This is the php code:
<?php
$statusMsg = '';
$msgClass = '';
if(isset($_POST['submit'])){
// Get the submitted form data
$email = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Check whether submitted data is not empty
if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$statusMsg = 'Please enter your valid email.';
$msgClass = 'errordiv';
}else{
// Recipient email
$toEmail = '_______@hotmail.com';
$emailSubject = 'Contact Request Submitted by '.$name;
$htmlContent = '<h2>Contact Request Submitted</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Subject</h4><p>'.$subject.'</p>
<h4>Message</h4><p>'.$message.'</p>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'errordiv';
}
}
}else{
$statusMsg = 'Please fill all the fields.';
$msgClass = 'errordiv';
}
}
?>
This is the css. code:
.contactFrm h4 {
font-size: 1em;
color: #252525;
margin-bottom: 0.5em;
font-weight: 300;
letter-spacing: 3px;
}
.contactFrm input[type="text"], .contactFrm input[type="email"] {
width: 40%;
color: #9370DB;
background: #fff;
outline: none;
font-size: 0.9em;
padding: .7em 1em;
border: 1px solid #9370DB;
-webkit-appearance: none;
display: block;
margin-bottom: 1.2em;
}
.contactFrm textarea {
resize: none;
width: 60%;
background: #fff;
color: #9370DB;
font-size: 0.9em;
outline: none;
padding: .6em 1em;
border: 1px solid #9370DB;
min-height: 10em;
-webkit-appearance: none;
}
.contactFrm input[type="submit"] {
outline: none;
color: #FFFFFF;
padding: 0.5em 0;
font-size: 1em;
margin: 1em 0 0 0;
-webkit-appearance: none;
background: #9370DB;
transition: 0.5s all;
border: 2px solid #795CB4;
-webkit-transition: 0.5s all;
transition: 0.5s all;
-moz-transition: 0.5s all;
width: 40%;
cursor: pointer;
}
.contactFrm input[type="submit"]:hover {
background: none;
color: #9370DB;
}
p.statusMsg{font-size:18px;}
p.succdiv{color: #008000;}
p.errordiv{color:#E80000;}
You have a couple of errors in your form code, which shouldn't make a difference, but put them right:
The first one is a double "", should be a single " -
<form id="contact" action="contact.php"" method="post">
The second one is you have repeated the opening form tag twice, so you need to delete the duplicated form tag -
<form action="" method="post">
Apart from that If you test the script on a remote server and that remote server is configured to use the php 'mail' function it should work as expect
...Copy link to clipboard
Copied
A quick glance does not show any errors in your code although the align="center" is a bit of a no-no in this era.
I am assuming that you are trying to send the email from you local system and that your local system does not have a mail server. The latter is certainly true on a Windows system as opposed to Unix/Linux
There are options
Copy link to clipboard
Copied
You have a couple of errors in your form code, which shouldn't make a difference, but put them right:
The first one is a double "", should be a single " -
<form id="contact" action="contact.php"" method="post">
The second one is you have repeated the opening form tag twice, so you need to delete the duplicated form tag -
<form action="" method="post">
Apart from that If you test the script on a remote server and that remote server is configured to use the php 'mail' function it should work as expected.
If it doesn't then the possibility is that your mail server doesnt allow you to use the php 'mail' function, check with your host.
Copy link to clipboard
Copied
When you sort out the errors, test it on your remote server with an email address on your web server instead of hotmail. you @ your_domain . com. I say that because hotmail might be blocking mail originating from your server. An email account on your hosting server will be less likely to block it.
Nancy