PHPMailer famous "Could not instantiate mail function." error

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) &lt; 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) &lt; 3) {
				$error['comments'] = "Please enter a message in the 'YOUR MESSAGE:' field.";
				$count = $count + 1;
			}
			
			if (!$resp-&gt;is_valid) {
				$error['captcha'] = "Please enter the correct CAPTCHA text from the displayed image."; 
				$count = $count + 1;
			}
				
			if (!$error && $resp-&gt;is_valid) {
				
				require_once('phpMailer/class.phpmailer.php');
				$mail = new PHPMailer();
				
				$mail-&gt;From = $email;
				$mail-&gt;FromName = $name;
				$mail-&gt;Subject = "Contact test";
				$mail-&gt;AddAddress($site_owners_email, $site_owners_name);
				$mail-&gt;AddAddress('info@website.com', 'Website name');
				$mail-&gt;Body = $comments;							
			
				$mail-&gt;Send();
				
				echo "&lt;h1&gt;Your email has been successfully sent!&lt;/h1&gt;";
				echo "&lt;p&gt; 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.&lt;/p&gt;";
				echo "&lt;div class='btn-blue-wide' style='margin-top: 20px'&gt;" ."&lt;a href='index.php'&gt; Return to home page&lt;/a&gt;"  ."&lt;/div&gt;";							
				
			} # end if no error
			else {
			
				if ( $count == 1 ) {
					$response = "&lt;h1&gt;Opps...you have " .$count  ." error!&lt;/h1&gt;";
				}
				else if ( $count &gt; 1 ) {
					$response = "&lt;h1&gt;Opps...you have " .$count  ." errors!&lt;/h1&gt;";
				}
							
				$response .= "&lt;p&gt;" ."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." ."&lt;/p&gt;&lt;br&gt;";
			
				$response .= "&lt;ul&gt;";
				$response .= (isset($error['name'])) ? "&lt;div id='contact-error'&gt;&lt;p&gt;&lt;img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /&gt; " . $error['name'] . "&lt;/p&gt;&lt;/div&gt;" : null;
				$response .= (isset($error['email'])) ? "&lt;div id='contact-error'&gt;&lt;p&gt;&lt;img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /&gt; " . $error['email'] . "&lt;/p&gt;&lt;/div&gt;" : null;
				$response .= (isset($error['comments'])) ? "&lt;div id='contact-error'&gt;&lt;p&gt;&lt;img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /&gt; " . $error['comments'] . "&lt;/p&gt;&lt;/div&gt;" : null;		
				$response .= (isset($error['captcha'])) ? "&lt;div id='contact-error'&gt;&lt;p&gt;&lt;img class='middle' src='/images/arrowright.png' width='24' height='24' alt='' /&gt; " . $error['captcha'] . "&lt;/p&gt;&lt;/div&gt;&lt;br&gt;" : null;		
				$response .= "&lt;/ul&gt;";		
				
				$response .= "&lt;p&gt;" ."In order to go back and fix these errors, either click your browser's back button or alternatively the button below." ."&lt;/p&gt;&lt;br&gt;";
				
				$response .= "&lt;div class='btn-blue' style='margin-top: 20px'&gt;" ."&lt;a href='javascript:history.go(-1)'&gt;Go back to fix it&lt;/a&gt;" ."&lt;/div&gt;";
				
				echo $response;
								
			} # end if there was an error sending

		?&gt;

Thanks in advance!

Few points to check:
1> is mail function disabled in your server?
This can be checked by using:

mail("YourEmail@domain.com", "Test Subject", "Test Message");  

2>is mail From param is valid?

$mail->From = $email;

3> did you try with SMTP?

$mail->IsSMTP();

Thanks for responding.

As for 1), I tried it and got the below error message:

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 C:\wamp\www\sendemail.php on line 137

I looked in php.ini for these lines and they’re correct, my ISP SMTP server, username, password, port number (25) are all correct. The odd thing is that just above these settings in php.ini is a comment line “; For Win32 only.” when I’m testing this via Windows XP x64.

As for 2) I have form validation so the from email will always be in the format abc@xyz.com

As for 3) I have added the line “$mail->IsSMTP();” just after the line “$mail = new PHPMailer();” and I get the following message (after a longer delay when I click the send email button on my contacts.php page (the sendmail.php code must be waiting for the SMTP server to timeout perhaps):

SMTP Error: Could not connect to SMTP host.

Would you have a link to a guide that says what needs to be checked when running PHPMailer() both on localhost and online? The PHP sending code looks okay, the php.ini looks okay, the PHPMailer class is correctly in “phpMailer/class.phpmailer.php”, I’m using my ISP’s STMP settings, what else must I have?

I must add that at the moment I’m testing it all offline under WAMP.

Many thanks,