Hello,
I have been coding a contacts page form using the PHPMailer method. The form is all done, validates input, sends that to be processed by a new instance of PHPMailer but this is where problems start to emerge. Namely just one problem, an error of “Could not instantiate mail function.”.
I’m aware this is a very popular problem and the answer is usually SMTP settings (from what I see online) however I’m somewhat confused where to look in the code. I’m testing this offline in the WAMP Apache environment which shouldn’t make a difference and I’ve also downloaded the latest version of PHPMailer (5.1) to which my code connects to (PHPMailer is in the folder “PHPMailer” in the root directory of the website files).
I would appreciate some pointers where should I be looking to fix this before I go uploading it. Do I just need to pass the SMTP settings in the mail setup below? Since SMTP doesn’t authenticate whether the email address I sent from is genuine I should be able to pass it anything such as 123@abc.com right? I don’t think it’s the port number as then it would say something like “cannot connect to smtp server” as far as I remember back from early 2009 when I had a similar albeit different problem.
Here’s the code in the page that processes the details ready to send off:
<?php
$name = trim($_POST['name']);
$email = $_POST['email'];
$comments = $_POST['comments'];
require_once('captcha/recaptchalib.php');
$privatekey = "Hidden";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$site_owners_email = 'info@website.co.uk'; // Replace this with your own email address
$site_owners_name = 'a site owner'; // replace with your name
$error = null;
$count = 0;
if (strlen($name) < 2) {
$error['name'] = "Please enter your name in the 'YOUR NAME:' field.";
$count = $count + 1;
}
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 in the 'YOUR EMAIL:' field.";
$count = $count + 1;
}
if (strlen($comments) < 3) {
$error['comments'] = "Please enter a message in the 'YOUR MESSAGE:' field.";
$count = $count + 1;
}
if (!$resp->is_valid) {
$error['captcha'] = "Please enter the correct CAPTCHA text from the displayed image.";
$count = $count + 1;
}
if (!$error && $resp->is_valid) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = "Contact test";
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->AddAddress('info@website.com', 'Website name');
$mail->Body = $comments;
$mail->Send();
echo "<h1>Your email has been successfully sent!</h1>";
echo "<p> Thank you " . $name . ". We've received your email. We'll be in touch as soon as we possibly can! Please allow up to three working days for a response.</p>";
echo "<div class='btn-blue-wide' style='margin-top: 20px'>" ."<a href='index.php'> Return to home page</a>" ."</div>";
} # end if no error
else {
if ( $count == 1 ) {
$response = "<h1>Opps...you have " .$count ." error!</h1>";
}
else if ( $count > 1 ) {
$response = "<h1>Opps...you have " .$count ." errors!</h1>";
}
$response .= "<p>" ."This has been caused by the fact that have you not supplied all the mandatory data required when filling out the fields, or that you have provided invalid data.
You will need to go back and fix the below listed errors before being able to submit your message to us." ."</p><br>";
$response .= "<ul>";
$response .= (isset($error['name'])) ? "<div id='contact-error'><p><img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /> " . $error['name'] . "</p></div>" : null;
$response .= (isset($error['email'])) ? "<div id='contact-error'><p><img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /> " . $error['email'] . "</p></div>" : null;
$response .= (isset($error['comments'])) ? "<div id='contact-error'><p><img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /> " . $error['comments'] . "</p></div>" : null;
$response .= (isset($error['captcha'])) ? "<div id='contact-error'><p><img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /> " . $error['captcha'] . "</p></div><br>" : null;
$response .= "</ul>";
$response .= "<p>" ."In order to go back and fix these errors, either click your browser's back button or alternatively the button below." ."</p><br>";
$response .= "<div class='btn-blue' style='margin-top: 20px'>" ."<a href='javascript:history.go(-1)'>Go back to fix it</a>" ."</div>";
echo $response;
} # end if there was an error sending
?>
Thanks in advance!