本地主机上的 php mail() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5342822/
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
php mail() function on localhost
提问by Shark
I have problem with php mail()
function on localhost server. I can't test my email application on localhost when i'm trying to send emails with php function
mail()
.
我mail()
在本地主机服务器上的php函数有问题。当我尝试使用 php 函数发送电子邮件时,我无法在本地主机上测试我的电子邮件应用程序
mail()
。
I'm getting this error
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 F:\wamp\www\product\ajax.php on line 64
我收到这个错误
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 F:\wamp\www\product\ajax.php on line 64
I'm using WAMP server and if anyone knows solution please help me.
我正在使用 WAMP 服务器,如果有人知道解决方案,请帮助我。
回答by Treffynnon
You need to setup a mail server on your machine for the mail function to work. If you are on Windows (which I am guessing you are from your use of WAMP) you can setup a Pegasus mail server.
您需要在您的机器上设置邮件服务器才能使用邮件功能。如果您使用的是 Windows(我猜您使用的是 WAMP),您可以设置Pegasus 邮件服务器。
Other options include using a wrapper class such as SwiftMaileror PHPMailerand using them to connect to another SMTP server such as your GMail account. Even if you go the Pegasus mail server on your own localhost route then I would still recommend using one of the two classes I have mentioned above. They give you far more flexibility and are safer.
其他选项包括使用包装类(例如SwiftMailer或PHPMailer)并使用它们连接到另一个 SMTP 服务器(例如您的 GMail 帐户)。即使您在自己的本地主机路由上使用 Pegasus 邮件服务器,我仍然建议使用我上面提到的两个类之一。它们为您提供更大的灵活性并且更安全。
Connecting to either your ISPs SMTP server or GMail or whatever is the easiest route out of this one.
连接到您的 ISP SMTP 服务器或 GMail 或任何最简单的路由。
回答by linuxhini
After spending 2 days on this php mail function issue I have figured it out and would help you do same. This has wasted enough time, Let's begin...
在这个 php 邮件功能问题上花了 2 天后,我已经想通了,并会帮助你做同样的事情。这已经浪费了足够的时间,让我们开始吧……
PHP has an in-built php mail function that can be used to send mail from script but this function is somehow limited because it cannot connect you to a simple mail transfer protocol (SMTP) server. I am assuming you are using WAML/XAMPP, haven't really tried XAMPP but you could figure that out following this:
PHP 有一个内置的 php 邮件功能,可用于从脚本发送邮件,但此功能在某种程度上受到限制,因为它无法将您连接到简单的邮件传输协议 (SMTP) 服务器。我假设您正在使用 WAML/XAMPP,还没有真正尝试过 XAMPP,但您可以通过以下方式弄清楚:
First we would have to find a way to connect our web server(wamp) to an external SMTP. We would be using Gmail in this case but before that, we need to download/install a tool that would grab our email from the php script and push it to the Gmail SMTP.
首先,我们必须找到一种方法将我们的 Web 服务器(wamp)连接到外部 SMTP。在这种情况下,我们将使用 Gmail,但在此之前,我们需要下载/安装一个工具,该工具将从 php 脚本中获取我们的电子邮件并将其推送到 Gmail SMTP。
- First go to http://glob.com.au/sendmail/and click on the download
sendmail.zip
to download. - After downloading, extract to
C:\wamp\
extract it as a folder meaning it's contents should be in a folder called sendmail and can be located atC:\wamp\sendmail
. - Now in the send mail folder, right click on
sendmail.ini
and open as an administrator since we are about to modify its contents. change the following lines
[sendmail] smtp_server=smtp.gmail.com smtp_port= 465 smtp_ssl=ssl default_domain=localhost error_logfile=error.log debug_logfile=debug.log auth_username= enter your gmail account here auth_password= enter the password for that account here ;pop3_server= ;pop3_username= ;pop3_password= ;force_sender= ;force_recipient= hostname= localhost
Please be careful and do as you see above. I have deleted most of the unwanted contents from my
sendmail.ini
file to make things a little easier to read.Save the file
Navigate to your
wamp
folder and move tobin\apache\apache[version here]\bin\php.ini
and modify the php file as an administrator as follows:If you are using notepad, you can use the find under the edit tab or ctrl+Fand type in "mail function"to quickly navigate to the mail function part of the
php.ini
file.[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 = ;For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;http://php.net/sendmail-path sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i" ----> this is the changed part.
Note: It is only the
sendmail_path
that you have to change to the above. Leave other settings as you see them. Your path maybe different depending on where you extracted the sendmail folder to.Save that
php.ini
fileMove back to
C:\wamp\bin
and chosephp\php[version here]\php.ini
and edit the sendmail path as you did above and save the file as an administrator/or before you open, open as an administrator by right clickingRun wampserver as an administrator and when it turns green turn on the following:
Left click on the green icon and move to php -> php extensions -> php_sockets (click to have the black check on it)
wait for the icon to turn green again and follow the above to turn on php_openssl.
If the icon turns green it is time to turn on apaches ssl_module by moving to the apache icom when you click on the green wampserver icon. You can find this at Apache modules.
When you are done with three steps above exit and run wampserver again as an admin to effect changes.
Move back to the sendmail extracted folder and right click on
sendmail.exe
Go to its properties -> compatibility -> change settings for all users -> compatibility mode [change to windows xp (service pack 3) ] -> Run this program as an administrator [check this option].Save and Apply.
We had to do this so that each time we call the sendmail App, it would run as an admin.Now we need to configure our gmail account to work with this sendmail App. Log into gmail account and at the top right corner click on the gear to choose settings. At settings move to Forwarding and POP/IMAP tab and enable IMAP. Save changes
Finally get back to your inbox, an the topmost right corner beside your email address is a little arrow pointing down, click on it to choose My Account. If you don't have that, you can try clicking on your profile picture to choose My Account from there.
Click on the Sign-in & security tab and scroll down to the bottom of the page. You would find Allow less secure apps: and turn it on. Mostly this would be off.
That's all you need to do so you can now send email from your php scripts on localhost. You can now try your
mail()
function with the 4 minimum parameters and see. Hope this helps. Vote if it helps so i can take time to post more interesting solutions.
- 首先去http://glob.com.au/sendmail/点击下载
sendmail.zip
进行下载。 - 下载后,解压以将其解
C:\wamp\
压缩为一个文件夹,这意味着它的内容应位于名为 sendmail 的文件夹中,并且可以位于C:\wamp\sendmail
. - 现在在发送邮件文件夹中,右键单击
sendmail.ini
并以管理员身份打开,因为我们将要修改其内容。 更改以下几行
[sendmail] smtp_server=smtp.gmail.com smtp_port= 465 smtp_ssl=ssl default_domain=localhost error_logfile=error.log debug_logfile=debug.log auth_username= enter your gmail account here auth_password= enter the password for that account here ;pop3_server= ;pop3_username= ;pop3_password= ;force_sender= ;force_recipient= hostname= localhost
请小心并按照上面的说明进行操作。我已经从我的
sendmail.ini
文件中删除了大部分不需要的内容,以使事情更容易阅读。保存文件
导航到您的
wamp
文件夹并以bin\apache\apache[version here]\bin\php.ini
管理员身份移动并修改 php 文件,如下所示:如果您使用的是记事本,您可以使用编辑选项卡下的查找或ctrl+F并输入“邮件功能”来快速导航到
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 = ;For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;http://php.net/sendmail-path sendmail_path = "C:\wamp\sendmail\sendmail.exe -t -i" ----> this is the changed part.
注意:只有
sendmail_path
您必须更改为上述内容。保留其他设置,如您所见。您的路径可能会有所不同,具体取决于您将 sendmail 文件夹解压缩到的位置。保存那个
php.ini
文件像上面一样返回
C:\wamp\bin
并选择php\php[version here]\php.ini
和编辑 sendmail 路径并将文件保存为管理员/或在打开之前,通过右键单击以管理员身份打开以管理员身份运行 wampserver,当它变成绿色时打开以下内容:
左键单击绿色图标并移至 php -> php extensions -> php_sockets(单击以选中黑色)
等待图标再次变绿,然后按照上面的方法开启php_openssl。
如果图标变成绿色,那么当您点击绿色的 wampserver 图标时,是时候通过移动到 apache icom 来打开 apaches ssl_module。您可以在 Apache 模块中找到它。
完成以上三个步骤后,退出并以管理员身份再次运行 wampserver 以实现更改。
移回 sendmail 提取的文件夹并右键单击
sendmail.exe
转到其属性 -> 兼容性 -> 更改所有用户的设置 -> 兼容模式 [更改为 windows xp(服务包 3)] -> 以管理员身份运行此程序 [检查这个选项]。保存并应用。
我们必须这样做,以便每次调用 sendmail 应用程序时,它都会以管理员身份运行。现在我们需要配置我们的 gmail 帐户以使用这个 sendmail 应用程序。登录 Gmail 帐户并在右上角单击齿轮以选择设置。在设置中移动到转发和 POP/IMAP 选项卡并启用 IMAP。保存更改
最后回到您的收件箱,您的电子邮件地址旁边的右上角是一个向下的小箭头,单击它以选择我的帐户。如果没有,您可以尝试单击您的个人资料图片,从那里选择我的帐户。
单击登录和安全选项卡并向下滚动到页面底部。您会找到允许不太安全的应用程序:并打开它。大多数情况下,这将关闭。
这就是您需要做的所有事情,现在您可以从本地主机上的 php 脚本发送电子邮件。您现在可以
mail()
使用 4 个最小参数尝试您的函数并查看。希望这可以帮助。投票是否有帮助,这样我就可以花时间发布更有趣的解决方案。
回答by Michael Berkowski
This is a well-known issue with using mail()
on Windows, where you typically don't have a local SMTP service. As your error message says, you need to define SMTP settings in your php.ini
to talk to a mail server through which you are allowed to send outbound messages, with or without authentication. See mail() docs.
这是mail()
在 Windows上使用的一个众所周知的问题,您通常没有本地 SMTP 服务。正如您的错误消息所说,您需要在您php.ini
的邮件服务器中定义 SMTP 设置,您可以通过该邮件服务器发送出站邮件,无论是否进行身份验证。 请参阅 mail() 文档。
回答by Technogupshup
Try to setup the mail server for localhost. Otherwise it'll not work in localhost.
尝试为本地主机设置邮件服务器。否则它将无法在本地主机中工作。
回答by Paul Coldrey
The easiest way I have found to create a mail sever in windows is to install Python and then use the inbuilt SMTP debug server with the following one-liner:
我发现在 Windows 中创建邮件服务器的最简单方法是安装 Python,然后使用带有以下单行的内置 SMTP 调试服务器:
python -m smtpd -c DebuggingServer -n localhost:25
On Windows, I have found that it sometimes has strange behaviour with localhost and 127.0.0.1 so, if all else fails, you could trying finding your IP address using the 'ipconfig' command (at the DOS command prompt) and you might see something like:
在 Windows 上,我发现 localhost 和 127.0.0.1 有时会出现奇怪的行为,因此,如果所有其他方法都失败了,您可以尝试使用“ipconfig”命令(在 DOS 命令提示符下)查找您的 IP 地址,您可能会看到一些内容喜欢:
c:\> ipconfig
-- stuff cut here ---
Wireless LAN adapter WiFi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::856d:fd33:3e01:e656%9
IPv4 Address. . . . . . . . . . . : 192.168.8.111
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.1
and, let's say you find you ip address is 192.168.8.11 (as I did in the example above), then changing the command to look like:
并且,假设您发现您的 IP 地址是 192.168.8.11(就像我在上面的示例中所做的那样),然后将命令更改为:
python -m smtpd -c DebuggingServer -n 192.168.8.11:25
And setting the following lines in your php.ini file:
并在您的 php.ini 文件中设置以下几行:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = 192.168.8.111
; http://php.net/smtp-port
smtp_port = 25
And, this has worked for me on occasions where nothing else does!
而且,这在没有其他任何作用的情况下对我有用!
I also note you can check if you local mail server is working by typing the command:
我还注意到,您可以通过键入以下命令来检查本地邮件服务器是否正常工作:
C:/> telnet localhost 25
or for my second configuration:
或者对于我的第二个配置:
C:/> telnet 192.168.8.111 25
Telnet comes for free on Linux but to get it on windows you have to go to "Programs and Features" and then click "Turn Windows feaures on and off" (from the little panel on the left) and then check the box for "Telnet Client" in the list of Windows Features.
Telnet 在 Linux 上免费提供,但要在 Windows 上使用它,您必须转到“程序和功能”,然后单击“打开和关闭 Windows 功能”(从左侧的小面板中),然后选中“Telnet”框客户端”在 Windows 功能列表中。
When you run the telnet command you will see something like this if the server is not running:
当您运行 telnet 命令时,如果服务器未运行,您将看到如下内容:
C:\Projects>telnet localhost 25
Connecting To localhost...Could not open connection to the host, on port 25: Connect failed
But, if it is all working you should get the screen cleared and a "server identifier" string back from the mail server. On my machine this looked like:
但是,如果一切正常,您应该清除屏幕并从邮件服务器返回“服务器标识符”字符串。在我的机器上,这看起来像:
220 ZOOT Python SMTP proxy version 0.3
If you are really keen you can type some SMTP commands and send a mail here or you can type 'quit' to disconnect from the server.
如果您真的很热衷,您可以在此处键入一些 SMTP 命令并发送邮件,或者您可以键入“退出”以断开与服务器的连接。