从本地主机的PHP在Windows发送电子邮件

问题描述:

我用这很小块代码来测试邮件是否到达电子邮件目标:从本地主机的PHP在Windows发送电子邮件

<?php 
    mail('[email protected]','Test mail','The mail function is working!'); 
    echo 'Mail sent!'; 
?> 

但它似乎没有奏效。我正在使用WAMP。我已经安装了一个免费的SMTP服务器。而我的php.ini文件配置为如下:

[mail function] 
; For Win32 only. 
; http://php.net/smtp 
SMTP = smtp.tools.sky.com 
; http://php.net/smtp-port 
smtp_port = 25 

; For Win32 only. 
; http://php.net/sendmail-from 
sendmail_from = [email protected] 

我不似乎可以接收电子邮件至[email protected]以下我所提到的行动。

我得到这个错误:

Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 
Must issue a STARTTLS command first. ff2sm10904265wib.9 in 
C:\wamp\www\Derrysgc2\pages\pages\mailtest.php on line 2 

有什么建议?

+0

对不起,请参阅上述错误 – user1278496 2012-04-06 14:02:17

+0

这听起来像是你有任何需要一个安全连接的SMTP服务器。我不确定PHP是否可以做到这一点。尝试一个本地SMTP。 – Ryan 2012-04-06 14:07:53

尝试使用SMTP服务器与Gmail。

ini_set("SMTP","ssl://smtp.gmail.com"); 
ini_set("smtp_port","465"); 

否则还有很多PHP邮件程序库。这可以简化您的工作,并使其更易于使用。我的收藏是迅捷的邮件。最好的部分是你不必乱搞你的核心php配置文件,而且文档也很容易阅读。

例如,如果你想发送邮件使用PHP的迅捷邮件程序库,它就像。

require_once 'lib/swift_required.php'; 

// Create the Transport 
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25) 
    ->setUsername('your username') 
    ->setPassword('your password'); 

// Create the Mailer using your created Transport 
$mailer = Swift_Mailer::newInstance($transport); 

// Create a message 
$message = Swift_Message::newInstance('Wonderful Subject') 
    ->setFrom(array('[email protected]' => 'John Doe')) 
    ->setTo(array('[email protected]', '[email protected]' => 'A name')) 
    ->setBody('Here is the message itself'); 

// Send the message 
$result = $mailer->send($message); 

你可以参考文档的详细信息在官方网站上http://swiftmailer.org/docs

不应该SMTP指向您的SMTP服务器? (我假设smtp.tools.sky.com是你的提供者)。 sendmail_from也应该是一个正确的电子邮件地址。

还要注意一些邮件提供程序阻止从动态IP地址发送邮件。

+0

所以我需要配置我的本地机器上安装的SMTP服务器? – user1278496 2012-04-06 14:09:12

+0

并且大多数时间您的ISP会阻止默认的SMTP端口,即端口25,您可以尝试将端口更改为465 – 2012-04-06 14:11:37

+0

如果您希望使用自己的SMTP服务器,那么您应该将php.ini指向本地服务器,然后使该服务器中继到您的提供商。 – Jaco 2012-04-06 16:47:12