2012-04-06 165 views
1

我用這很小塊代碼來測試郵件是否到達電子郵件目標:從本地主機的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

回答

4

嘗試使用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

1

不應該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