I just purchased a website template from DreamTemplate.com, and have tried to send a test e-mail from it online and instead of sending the e-mail it just comes up with red text saying: “ERROR!”.
I am not very familiar with PHP, but I can understand the syntax. The code is below:
Contact.html
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function () {
$('#contactform').submit(function () {
var action = $(this).attr('action');
$.post(action, {
name: $('#name').val(),
email: $('#email').val(),
company: $('#company').val(),
subject: $('#subject').val(),
message: $('#message').val()
}, function (data) {
$('#contactform #submit').attr('disabled', '');
$('.response').remove();
$('#contactform').before('<p class="response">' + data + '</p>');
$('.response').slideDown();
if (data == 'Message sent!') $('#contactform').slideUp();
});
return false;
});
});
// ]]>
</script>
Contact.PHP
<?php
if(!$_POST) exit;
$email = $_POST['email'];
//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="You've entered an invalid e-mail address.";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','message');
$required = array('name','email','message');
$your_email = "enquiries@ourdomain.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "new message:\n";
foreach($values as $key => $value){
if(in_array($value,$required)){
if ($key != 'subject' && $key != 'company') {
if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
}
$email_content .= $value.': '.$_POST[$value]."\n";
}
}
if(@mail($your_email,$email_subject,$email_content)) {
echo 'Message sent!';
} else {
echo 'ERROR!';
}
}
?>
Can someone please help? I have no idea why this won’t work. Any help at all is much appreciated.
Thank you
Update:
As advised, I have removed the @ symbol before ‘mail’. It is now displaying useful error messages:
“Warning: mail() [function.mail]: Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini or use ini_set() in E:\web\autoopti\contact.php on line 31
ERROR!”
Posting another answer based on the OP’s edit.
Are you running this on your local machine, or on another Windows server? You need to have a SMTP server running on the machine you’re trying to execute mail() from. Please see these articles: One Two for basic instructions on setting up an SMTP server, or Google has the answers.
Quick explanation of what an SMTP server is and does: it’s the mail relayer. The SMTP server implements the messaging protocols to route e-mails to their appropriate destination(s).
EDIT: If you feel like diving headfirst into PHP, and don’t want to bother setting up a local SMTP server, you can use PHPMailer to use your ISP’s or another remote SMTP server, where it’s already configured for you. There’s plenty of sample code out there.
Hope that helps!