我能否配置WAMP服务器以启用PHP的mail()
函数?
在本地主机上配置一个可用的电子邮件客户端是非常繁琐的,我花了很多时间和精力才尝试完成。我相信有更有经验的人可以帮忙,或者他们可能会同意我的看法。
如果你只是想测试一下,这里有一个非常好用的工具可以在本地测试邮件,几乎不需要任何配置:
安装Fake Sendmail(下载sendmail.zip)。 然后配置C:\wamp\sendmail\sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=user@gmail.com
auth_password=your_password
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
mail
的PHP页面后,您将收到来自Google的电子邮件,提示有一次登录尝试被阻止。在该电子邮件中,点击“检查活动”,然后在打开的页面上点击“Yes, it was me”,然后点击“了解更多”,再点击“If 'Less secure app access' is off for your account”,然后点击“turn it back on”,最后打开开关即可。 - Donald Duckmail
函数时,您必须指定一个 from
头。例如,您可以这样做:mail("to@example.com", "Subject", "Content", "From: youremailaddress@gmail.com")
(最后一个参数很重要)。 - Donald Duck要发送电子邮件,您需要使用SMTP服务器。如果您有一个不需要SMTP身份验证的可用服务器(可能是您的ISP提供的),只需编辑php.ini文件中的“SMTP”([mail function])设置。
如果您的SMTP服务器需要身份验证,则无法使用内部的mail()函数,并且必须使用支持smtp auth的一些第三方类库,例如http://pear.php.net/package/Mail/
由于Sendmail无法正常工作,我使用了msmtp 1.6.2 w32并且按照DeveloperSide的说明进行了操作。以下是设置过程的简要介绍:
在您的Gmail帐户下启用IMAP(msmtp发送电子邮件的帐户之一)
启用不太安全应用程序的访问权限。登录您的Google帐户并前往这里
编辑php.ini
,找到并更改每个设置以反映以下内容:
; These are commented out by prefixing a semicolon
;SMTP = localhost
;smtp_port = 25
; Set these paths to where you put your msmtp files.
; I used backslashes in php.ini and it works fine.
; The example in the devside guide uses forwardslashes.
sendmail_path = "C:\wamp64\msmtp\msmtp.exe -d -C C:\wamp64\msmtp\msmtprc.ini -t --read-envelope-from"
mail.log = "C:\wamp64\msmtp\maillog.txt"
msmtp.exe
文件相同的目录中创建并编辑文件msmtprc.ini
,如下所示,将其替换为您自己的电子邮件和密码:# Default values for all accounts
defaults
tls_certcheck off
# I used forward slashes here and it works.
logfile C:/wamp64/msmtp/msmtplog.txt
account Gmail
host smtp.gmail.com
port 587
auth on
tls on
from ReplaceWithYourEmail@gmail.com
user ReplaceWithYourEmail@gmail.com
password ReplaceWithYourPassword
account default : gmail
msmtp-1.6.2-w32.zip
文件。
https://sourceforge.net/projects/msmtp/files/msmtp/1.6.2/ - iyrin不需要安装和配置自己的SMTP服务器,可以(更简单地)使用个人的Gmail邮箱,并配置PHP/WAMP来使用它并通过它发送电子邮件。
截至2023年,Gmail默认不支持个人用户的SMTP。因此,一些早期的答案似乎已经过时了。现在似乎需要先启用“双重身份验证”,然后生成“应用程序密码”。
所以让我们先配置Google端:
https://github.com/sendmail-tls1-2/main
我使用了这个文件来确保准确性: Sendmail_v33_TLS1_2.zip 解压并配置sendmail.ini
如下(不要忘记将auth_username
和auth_password
更改为您的电子邮件和密码):smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
; note this is useful to uncoment and peek if something doesnt work
; debug_logfile=debug.log
auth_username=yourEmail@gmail.com
auth_password= that-16-letters-password-you-got-from-Google-App-Password
为了测试sendmail是否正常工作,请在sendmail文件夹中创建test.txt
。像这样(但请编辑您的接收电子邮件地址):
From: anything@example.com
To: FillYourReceive@email.com
Subject: Your Subject
Your Body message
Your Body message second line
sendmail
文件夹并运行它:sendmail.exe < test.txt
error.log
文件或在sednmail.ini
中启用调试文件,并同时检查debug.log
文件。php.ini
(wamp图标->PHP->php.ini [apache模块]
)。;SMTP = localhost
;smtp_port = 25
sendmail_from ="admin@wampserver.vbe"
sendmail_path = "c:/wamp64/bin/Sendmail_v33_TLS1_2/sendmail.exe -t"
;mail.force_extra_parameters =
mail.add_x_header = Off
sendmail_path
为您拥有的sendmail.exe
的位置,并将其最佳复制到WAMP
文件夹中。wamp图标->Apache->服务管理'wampapache64'->重新启动服务
然后在您的文档根目录中创建一个名为test_mail.php
的网页,其中包含以下数据。(只需记得更改FillYOurReceiveEmail
):<?php
echo "<br>sendmail_path:".strval(ini_get('sendmail_path'));
$from = "anything@anywhere.com";
$to = "FillYOurReceiveEmail@gmail.com";
$subject = "Your email subject";
$message = "Your email body message";
$headers = [ "From: $from" ];
$ret = mail( $to, $subject, $message, implode( '\r\n', $headers ) );
// OR - PHP 7.2.0 or greater
//mail( $to, $subject, $message, $headers );
echo "<br>ret :".strval($ret);
?>
localhost/test_mail.php
你会看到类似以下内容:sendmail_path:c:/wamp64/bin/Sendmail_v33_TLS1_2/sendmail.exe
ret :1
您将收到一封发送至您填写的电子邮件的邮件。
我使用Mercury/32和Pegasus Mail来使mail()功能正常工作。如果您想要一个以您的域名结尾的电子邮件地址,它也非常适合用作邮件服务器。
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=youremail@gmail.com
auth_password= here place the gmail generated app code not your email password
SMTP = localhost
smtp_port = 587
sendmail_from = youemail@gmail.com
auth_username = youremail@gmail.com
auth_password = here place the same gmail generated app code not your email password
sendmail_path = path\to\Ampps\sendmail\sendmail.exe