php - 帮助使用 PHP mail() 函数

标签 php email

<分区>

Possible Duplicate:
php mail() function on localhost

我正尝试在我的站点上进行一些本地主机测试以恢复密码,但是当我尝试发送电子邮件时,出现以下错误:

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()

这是我的php.ini文件中的相关设置。

; 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 = you@yourdomain

我不确定为本地主机测试设置什么。我意识到我需要将 SMTP 设置为我的提供商的邮件服务器,但我在共享办公大楼工作,所以我什至不知道如何找出谁在这里提供互联网。

提前致谢。

最佳答案

PHP 的 mail() 函数不直接实现 SMTP 协议(protocol)。相反,它依赖于 sendmail() MTA(SMTP 服务器)或类似 postfix 或 mstmp 的替代品。 只要安装了 MTA,它就可以在 Unix 上正常工作。

在 Windows 上(来自 PHP.net 手册):

The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).

所以 - 这个故事的寓意 - 您需要安装邮件服务器。

但是 - 如果它仅用于测试目的 - 只需获取一个实际实现 SMTP 协议(protocol)的 PHP 库并使用您的常规 gmail 电子邮件地址发送电子邮件:

不使用 PHP 的 mail(),而是使用其中之一:

  1. PHP邮件程序
  2. SwiftMailer
  3. Zend\邮件

这些 PHP 库实际上实现了 SMTP 协议(protocol),因此可以轻松地从任何平台发送电子邮件,而无需在同一台机器上安装电子邮件服务器:

PHPMAILER 示例:

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "some_email@gmail.com";  // GMAIL username
$mail->Password   = "pass111";            // GMAIL password
$mail->SetFrom('some_email@gmail.com', 'My name is slim shady');
$mail->AddReplyTo("some_email@gmail.com","My name is slim shady");
$mail->Subject    = "Hey, check out http://www.site.com";
$mail->AltBody    = "Hey, check out this new post on www.site.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "some_email@gmail.com";
$mail->AddAddress($address, "My name is slim shady");

关于php - 帮助使用 PHP mail() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846567/

相关文章:

php - Mod_rewrite 在 url 中返回空格

php - 未定义索引 : when processing form with multiple selections

java - 使用 javax.mail 删除服务器上的电子邮件

php - PHP PEAR Mail->发送成功/失败结果?

php - Laravel mailtrap 无法在本地主机上工作

php - $wpdb 查询从匹配特定 ID 的列中对 SQL 小数求和

javascript - 如何在 React 路由器上使用 Laravel 路由

linux - 推荐电子邮件客户端读取/应用 git 补丁?

android - 如何在不需要 gmail 的情况下从电子邮件链接到 Android 应用程序?

c# - 如何在使用 ASP.NET Identity 确认帐户后执行自动登录