使用 sendmail 从 localhost/WAMP 服务器发送电子邮件
此解决方案需要 sendmail.exe(一个命令行界面 (CLI) 可执行文件,它接受来自 PHP 的电子邮件,连接到 SMTP 服务器并发送电子邮件)。你不需要通过命令来使用它,不用担心它:-) Download the sendmail.zip 并按照以下步骤操作:
Create a folder named “sendmail” in “C:\wamp\”.
Extract these 4 files in “sendmail” folder: “sendmail.exe”, “libeay32.dll”, “ssleay32.dll” and “sendmail.ini”.
Open the “sendmail.ini” file and configure it as following
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=ssl
default_domain=localhost
error_logfile=error.log
debug_logfile=debug.log
auth_username=[your_gmail_account_username]@gmail.com
auth_password=[your_gmail_account_password]
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=localhost
您不需要为这些属性指定任何值:pop3_server、pop3_username、pop3_password、force_sender、force_recipient。如果您已经发送成功的电子邮件,则 error_logfile 和 debug_logfile 设置应保持空白,否则此文件的大小将不断增加。如果您无法使用 sendmail 发送电子邮件,请启用这些日志文件设置。
在您的 GMail 设置 -> 转发和 POP/IMAP -> IMAP 访问中启用 IMAP 访问
在 Apache 服务器中启用“ssl_module”模块
为 PHP 编译器启用“php_openssl”和“php_sockets”扩展
从“C:\wamp\bin\apache\Apache2.2.17\bin”打开php.ini,配置如下
(“C:\wamp\bin\php\php5.3.x”中的 php.ini 不起作用)
(您只需要在下面的代码中配置最后一行,在其他行前面加上分号(;)
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP =
; http://php.net/smtp-port
;smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = you@domain.com
; 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"
重新启动 WAMP 服务器。
创建一个 PHP 文件并在其中写入以下代码:
<?php
$to = 'recipient@yahoo.com';
$subject = 'Testing sendmail.exe';
$message = 'Hi, you just received an email using sendmail!';
$headers = 'From: sender@gmail.com' . "\r\n" .
'Reply-To: sender@gmail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers))
echo "Email sent";
else
echo "Email sending failed";
?>
对 $to 和 $headers 变量进行适当的更改以设置收件人、发件人和回复地址。将其保存为“send-mail.php”。 (您可以将其保存在“C:\wamp\www”中的任何位置或任何子文件夹中。)
在浏览器中打开此文件,它现在必须工作
参考链接http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/