1
votes

I am using localhost WAMP server (Windows) for webhosting. I am getting the following error.

phpmailer ERROR :Could not instantiate mail function.

I have tried many solutions but nothing worked for me.

PHP mailing script

    $mailer = new PHPMailer();
    $mailer->IsMAIL();
    $mailer->CharSet = 'utf-8';
    $mailer->AddAddress($formvars['email'],$formvars['name']);
    $mailer->Subject = "Your registration with ".$this->sitename;
    $mailer->From = $this->GetFromAddress();        
    $confirmcode = $formvars['confirmcode'];
    $confirm_url = $this->GetAbsoluteURLFolder().'/confirmreg.php?code='.$confirmcode;
    $mailer->Body ="Hello ".$formvars['name']."\r\n\r\n".
    "Thanks for your registration with ".$this->sitename."\r\n".
    "Please click the link below to confirm your registration.\r\n".
    "$confirm_url\r\n".
    "\r\n".
    "Regards,\r\n".
    "Webmaster\r\n".
    $this->sitename;

    if(!$mailer->Send())
    {
        $this->HandleError("Failed sending registration confirmation email.".$mailer->ErrorInfo);
        //echo "Mailer Error: " . $mailer->ErrorInfo;
        return false;
    }
    return true;

How do I diagnose this error?

2
Did you require_once ('your_path/phpmailer/PHPMailerAutoload.php');?Jonatas Walker
Has $formvars array been properly set?Adam T
Souds like it is error-ing out on IsMail() which uses PHP's built in mail function. Have you verified that your php.ini mail settings and sendmail (if you are using that) are setup properly?jnthnjns
@jonataswalker not including PHPMailer would give a class not found, not a 'Could not instantiate mail function'. That should not have been upvoted.jnthnjns
@JonatasWalker Yeah I did.tarun14110

2 Answers

2
votes

$mailer->IsMAIL(); tells PHPMailer to use the PHP mail() function for emails delivery. It works out of the box on Unix-like OSes but not on Windows because the desktop versions of Windows do not install a SMTP server (it is available on the installation disc).

In order to make it work on Windows you have to change the PHP configuration. Either you edit php.ini (the configuration will apply globally) or you can use function ini_set() to change it only for current execution of the current script.

The best way is to change php.ini. You can find it in the PHP's installation directory or in the Windows's directory. Check the output of PHP function phpinfo() to find out its exact location.

Open php.ini in a text editor (the one you use to write the code is the best) and search for a section that looks like this:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]

For SMTP, use the name or the IP address of your company's mail server. Or your ISP's mail server if you are at home. Or check the configuration of your email client.

The smtp_port is either 25 for standard SMTP or 587 if the server uses SSL to encrypt its communication.

Also uncomment the sendmail_from setting if it is commented out (remove the ; from the beginning of line) and put your email address there.

Read more about the PHP configuration for sending emails on Windows on the documentation page.

Another option is to install a SMTP server on the computer. You can find one on the Windows installation disc or you can use a third-party solution.


If you choose to not install a SMTP server on the computer you don't even need to modify php.ini. You can change the code of the script to use a SMTP server:

$mailer = new PHPMailer();

$mailer->IsSMTP();
$mailer->Host = "mail.example.com";
$mailer->Port = 25;

Check this PHPmailer example for all the settings and their meaning.

If the SMTP server uses SMTP authentication (the webmail providers do it, your ISP or your company might do it or might not do it) then you have to also add:

$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "yourpassword";

If you use Gmail's SMTP server, for example, then use your Gmail email address and password. Replace "Gmail" with "Hotmail" or "Yahoo!" or your webmail provider or your company in the sentence above.

0
votes

Configuring a working mail client from localhost is always a mission and often "unecessary" considering it's just a sandbox. Although some developers want to solve the puzzle (I am one of those), some look for an easier alternative.

I would suggest that you install HMailServer

I have seen lots of people having great luck at a very fast speed using hmail.

Check this post for the step by step: How to send email from localhost WAMP Server to send email Gmail Hotmail or so forth?