I've implemented a contact form on a website and it's utilising php and the phpmailer class to send the mails via my hosts smtp servers.
When I submit the form I get the following error message:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
Here's the full page of code I'm using ...
<?php
session_start();
$name = trim($_POST['name']);
$email = $_POST['email'];
$comments = $_POST['comments'];
$captcha = $_POST['captcha'];
$site_owners_email = 'myemail.com';
$site_owners_name = 'my name';
if (strlen($name) < 2) {
$error['name'] = "Please enter your name";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address";
}
if (strlen($comments) < 3) {
$error['comments'] = "Please leave a comment";
}
if (int($captcha) !== ($_SESSION['randomnr2'])) {
$error['captcha'] = "CAPTCHA error. Please try again";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Contact Form";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $comments;
// Mail Server Settings
$mail->Mailer = "smtp";
$mail->Host = "myhost.com";
$mail->Port = "25";
$mail->SMTPSecure = "tls";
$mail->SMTPAuth = true;
$mail->Username = "myname.com";
$mail->Password = "mypassword";
$mail->Send();
echo "<li class='success'> Thank you " . $name . ". We've received your email. We'll be in touch with you as soon as we possibly can! </li>";
} # end if no error
else {
$response = (isset($error['name'])) ? "<li>" . $error['name'] . "</li> \n" : null;
$response .= (isset($error['email'])) ? "<li>" . $error['email'] . "</li> \n" : null;
$response .= (isset($error['comments'])) ? "<li>" . $error['comments'] . "</li>" : null;
$response .= (isset($error['captcha'])) ? "<li>" . $error['captcha'] . "</li>" : null;
echo $response;
} # end if there was an error sending
?>
The form is working so the php is, for the most part fine. I send a message through the form and I receive it in my inbox.