Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

php - phpmailer ERROR :Could not instantiate mail function

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']."

".
    "Thanks for your registration with ".$this->sitename."
".
    "Please click the link below to confirm your registration.
".
    "$confirm_url
".
    "
".
    "Regards,
".
    "Webmaster
".
    $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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...