1

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?

Community
  • 1
  • 1
tarun14110
  • 940
  • 5
  • 26
  • 57
  • 1
    Did you `require_once ('your_path/phpmailer/PHPMailerAutoload.php');`? – Jonatas Walker Jun 04 '15 at 15:53
  • Has `$formvars` array been properly set? – Adam T Jun 04 '15 at 16:03
  • 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 Jun 04 '15 at 16:11
  • @jonataswalker not including PHPMailer would give a class not found, not a 'Could not instantiate mail function'. That should not have been upvoted. – jnthnjns Jun 04 '15 at 16:14
  • @JonatasWalker Yeah I did. – tarun14110 Jun 04 '15 at 16:16
  • @AdamT I have also tried hard coded strings. Then also not working. So `$formvars ` shouldn't be the problem. – tarun14110 Jun 04 '15 at 16:19
  • @tarun14110 how far does the code get before encounters the error? Since there are functions internal to `$mailer` I would at first agree with Aosk in that IsMail() could be throwing the error. Since I'm unfamiliar with the mailer you're using, I would not know what the role of IsMail is supposed to be or if it is needed. – Adam T Jun 04 '15 at 16:25
  • possible duplicate of [Could not instantiate mail function. Why this error occuring](http://stackoverflow.com/questions/1944631/could-not-instantiate-mail-function-why-this-error-occuring) – Synchro Jun 04 '15 at 17:00

2 Answers2

2

$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 = me@example.com

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 = "yourname@example.com";
$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.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • +1, I don't think you *have* to set `sendmail_from` in the php.ini file. I've never had to do so, just include it on the PHP side. Also if you use PHPMailer `isSMTP()` you have to also include `$mailer->Username = 'yourSMTPusername@yourdomain.com'` and `$mailer->Password = 'yourPassword'` – jnthnjns Jun 04 '15 at 16:48
  • @Asok you don't necessarily need to do that. If you're sending directly to a recipient's server, it will usually accept unauthenticated connections on port 25. If you're relaying via somwehere else (like gmail), then you will indeed need to authenticate first. – Synchro Jun 04 '15 at 16:59
  • @Asok the SMTP username and password are required by some SMTP servers. The webmail providers ask for them for sure. Your ISP might ask or might not ask for them. Also your company mail server. – axiac Jun 04 '15 at 17:00
0

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?

Community
  • 1
  • 1
Raphael Costa
  • 86
  • 1
  • 3