Reputation: 1
I've written a php contact form and can't figure out why it is not sending an email. The form submits fine but doesn't send an actual email.
The below is my PHP code.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$/i", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = '[email protected]';
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
The HTML code below is the Contact form HTML:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactform">
<fieldset class="contact-fieldset">
<h2>Send us a message</h2>
<ul>
<li>
<label for="contactname">Your Name:</label>
<div class="ctinput-bg"><input type="text" name="contactname" id="contactname" value="" class="contact-input required" /></div>
</li>
<li>
<label for="email">Email:</label>
<div class="ctinput-bg"><input type="text" id="email" name="email" class="contact-input required email" /></div>
</li>
<li>
<label for="subject">Subject:</label>
<div class="ctinput-bg"><input type="text" name="subject" id="subject" class="contact-input required" /></div>
</li>
<li>
<label for="message">Your message:</label>
<div class="cttxtarea-bg"><textarea rows="6" cols="40" id="message" name="message" class="contact-textarea required"></textarea></div>
</li>
<li>
<div class="form-button contact-submit">
<span><input type="submit" value=" send message " name="submit" /></span>
</div>
</li>
</ul>
</fieldset>
</form>
Upvotes: 0
Views: 574
Reputation: 88
On top of the solutions others have provided I would highly recommend using the SwiftMailer library.
I had to create an automated email feature and it was a nightmare to manually set up the headers and such. SwiftMailer saved me a heap of time, see here for an example.
Upvotes: 1
Reputation: 1819
You can try to check if the mail is actually send:
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
And to be sure you can also do a var_dump of your headers to see if they are correct:
var_dump($headers);
These things won't solve the problem, but will surely help to debug it.
Upvotes: 0
Reputation: 147
I see 2 things :
1- the declairation of $hasError = false in the top of your code (make shure it excist)
2 - Generate headers as this :
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Hope this will do the trick,
Upvotes: 0
Reputation: 155145
Sounds like your mail
function isn't set up properly. Check your php.ini
file and ensure you have the correct SMTP server settings.
Also, unrelated to your original question, but I'm not sure that using <ul>
to markup a form is semantically correct. A set of fields isn't really an unsorted list, I'd prefer to use a semantically-correct <table>
or even a series of <div>
s.
Upvotes: 0