2012-11-09 71 views
0

我嘗試從本地主機發送郵件,但它不能正常工作。下面給出我的代碼。請看下面的代碼,並請告訴實際問題是什麼。由於無法從本地主機發送SMTP郵件()

郵件功能我用 - >

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

; For Win32 only. 
; http://php.net/sendmail-from 
;sendmail_from = NULL 

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). 
; http://php.net/sendmail-path 
;sendmail_path = NULL 

; Force the addition of the specified parameters to be passed as extra parameters 
; to the sendmail binary. These parameters will always replace the value of 
; the 5th parameter to mail(), even in safe mode. 
;mail.force_extra_parameters = NULL 

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename 
mail.add_x_header = Off 

; Log all mail() calls including the full path of the script, line #, to address and headers 
mail.log = "D:\xampp\apache\logs\php_mail.log" 

PHP代碼中使用 - >

<?php 
$to = "[email protected], [email protected]"; 
$subject = "HTML email"; 

$message = " 
<html> 
<head> 
<title>HTML email</title> 
</head> 
<body> 
<p>This email contains HTML Tags!</p> 
<table> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr> 
<tr> 
<td>John</td> 
<td>Doe</td> 
</tr> 
</table> 
</body> 
</html> 
"; 

// Always set content-type when sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 

// More headers 
$headers .= 'From: <[email protected]>' . "\r\n"; 
$headers .= 'Cc: mybo[email protected]' . "\r\n"; 

mail($to,$subject,$message,$headers); 
?> 

得到錯誤當執行PHP代碼上文>

Warning: mail() [function.mail]: Failed to connect to mailserver at "SMTP" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\xampp\htdocs\mail.php on line 43 

在此先感謝。

+1

您使用的是什麼localhost程序?例如WAMP,XAMPP或本地VM? – Jelmer

+0

我使用了XAMPP服務器。 – Vaibhav

+0

您需要指定SMTP服務器的域或IP地址。在您的php.ini文件中是「SMTP」對實際設置的本地SMTP服務器的引用?如果沒有,您需要安裝並設置SMTP服務器。您可以從網上下載免費的SMTP服務器。 –

回答

3

還有就是你的SMTP端口25上運行,您可以使用自帶的IIS SMTP服務器,或者您可以使用類似http://www.xmailserver.org/http://www.hmailserver.com

服務器管理器>功能(不角色)>沒有郵件服務器右對齊單擊添加> SMTP服務器 - 然後將IIS 7 SMTP配置爲指向本地服務器。

+0

我必須先安裝郵件程序。 – Vaibhav

+1

是的,您需要先安裝郵件程序才能發送郵件。正如Imran Azad在評論中所建議的那樣,XAMPP附帶了一個您可以使用的SMTP服務器, – ajtrichards

+0

ok謝謝您寶貴的建議。 – Vaibhav

0

下面是一個簡單的python腳本,它可以讓你在本地主機上運行一個郵件服務器,你不需要在php.ini中改變任何東西。

import smtpd 

import smtplib 

import asyncore 

class SMTPServer(smtpd.SMTPServer): 

    def __init__(*args, **kwargs): 
     print "Running smtp server on port 25" 
     smtpd.SMTPServer.__init__(*args, **kwargs) 

    def process_message(*args, **kwargs): 
     to = args[3][0] 
     msg = args[4] 
     gmail_user = 'yourgmailhere' 
     gmail_pwd = 'yourgmailpassword' 
     smtpserver = smtplib.SMTP("smtp.gmail.com",587) 
     smtpserver.ehlo() 
     smtpserver.starttls() 
     smtpserver.ehlo 
     smtpserver.login(gmail_user, gmail_pwd) 
     smtpserver.sendmail(gmail_user, to, msg) 
     print 'sent to '+to 
     pass 
if __name__ == "__main__": 
    smtp_server = SMTPServer(('localhost', 25), None) 
    try: 
     asyncore.loop() 
    except KeyboardInterrupt: 
     smtp_server.close() 

#end of code 

注:我用ARGS [3] [0]和args [4]如,以處理和消息作爲()由我的PHP郵件發送的ARGS對應成參數的陣列[3] [0]作爲接收郵件

相關問題