php phpmailer 错误:无法实例化邮件功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30648462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
phpmailer ERROR :Could not instantiate mail function
提问by tarun14110
I am using localhost WAMP server (Windows) for webhosting. I am getting the following error.
我正在使用 localhost WAMP 服务器 (Windows) 进行虚拟主机。我收到以下错误。
phpmailer ERROR :Could not instantiate mail function.
phpmailer 错误:无法实例化邮件功能。
I have tried many solutions but nothing worked for me.
我尝试了很多解决方案,但没有任何效果对我有用。
PHP mailing script
PHP邮件脚本
$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?
如何诊断此错误?
回答by axiac
$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).
$mailer->IsMAIL();
告诉PHPMailer
使用 PHPmail()
函数发送电子邮件。它在类 Unix 操作系统上开箱即用,但不能在 Windows 上运行,因为 Windows 的桌面版本不安装 SMTP 服务器(可在安装光盘上找到)。
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.
为了使其在 Windows 上工作,您必须更改 PHP 配置。您可以编辑php.ini
(配置将全局应用),或者您可以使用 functionini_set()
仅针对当前脚本的当前执行来更改它。
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.
最好的办法就是改变php.ini
。您可以在 PHP 的安装目录或 Windows 的目录中找到它。检查 PHP 函数的输出phpinfo()
以找出其确切位置。
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:
php.ini
在文本编辑器中打开(您用来编写代码的最好)并搜索如下所示的部分:
[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.
对于SMTP
,请使用您公司邮件服务器的名称或 IP 地址。或者如果你在家,你的 ISP 的邮件服务器。或者检查您的电子邮件客户端的配置。
The smtp_port
is either 25
for standard SMTP
or 587
if the server uses SSL
to encrypt its communication.
该smtp_port
要么是25
标准SMTP
或者587
如果服务器使用SSL
加密其通信。
Also uncomment the sendmail_from
setting if it is commented out (remove the ;
from the beginning of line) and put your email address there.
sendmail_from
如果设置被注释掉(;
从行首删除),也取消注释设置并将您的电子邮件地址放在那里。
Read more about the PHP configuration for sending emails on Windows on the documentation page.
在文档页面上阅读有关在 Windows 上发送电子邮件的 PHP 配置的更多信息。
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.
另一种选择是在计算机上安装 SMTP 服务器。您可以在 Windows 安装光盘上找到一个,也可以使用第三方解决方案。
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:
如果您选择不在计算机上安装 SMTP 服务器,您甚至不需要修改php.ini
. 您可以更改脚本的代码以使用 SMTP 服务器:
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = "mail.example.com";
$mailer->Port = 25;
Check this PHPmailer examplefor all the settings and their meaning.
检查此 PHPmailer 示例以了解所有设置及其含义。
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:
如果 SMTP 服务器使用 SMTP 身份验证(网络邮件提供商这样做,您的 ISP 或您的公司可能会这样做,也可能不会这样做),那么您还必须添加:
$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.
例如,如果您使用 Gmail 的 SMTP 服务器,请使用您的 Gmail 电子邮件地址和密码。将“Gmail”替换为“Hotmail”或“Yahoo!” 或您的网络邮件提供商或您的公司在上面的句子中。
回答by Raphael Costa
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
我建议你安装HMailServer
I have seen lots of people having great luck at a very fast speed using hmail.
我看到很多人使用 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?
一步一步检查这篇文章:How to send email from localhost WAMP Server to send email Gmail Hotmail等?