2013-08-17 134 views
15

我是新的PHP。我試圖使用此代碼從PHP發送郵件。php從本地主機發送郵件

<?php 

    $to  = '[email protected]'; 
    $subject = 'The subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 

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

?> 

我在php.ini

[mail function] 
; 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 = [email protected] 

&在sendmail.ini

# A freemail service example 
account Gmail 
tls on 
tls_certcheck off 
host smtp.gmail.com 
from [email protected] 
auth on 
user [email protected] 
password xxxxxxxxx 

# Set a default account 
account default : Gmail 

更改設置現在的代碼成功運行,但我沒有收到任何郵件。

+3

您必須在本地主機上安裝郵件服務器。 [PHPMailer](http://sourceforge.net/projects/phpmailer/) – Bora

+0

@Bora:我認爲可以添加Gmail或雅虎smtp郵件服務器。不是嗎? –

+0

或使用遠程SMTP服務器,如GMAIL – 2013-08-17 11:00:16

回答

1

該功能在您的本地主機上不起作用,因爲locahost不能用作SMTP服務器,請將您的內容上傳到安裝了SMTP的有效服務器,然後進行郵件調用。

0

您的服務器沒有本地郵件服務器。

有幾個解決方案:

  • 安裝本地郵件服務器,如果你有足夠的權限
  • 更改您的PHP設置使用其他郵件服務器(其他開放郵件服務器或身份驗證爲基礎的人,如Gmail,雅虎等)
  • 使用支持IMAP/POP3的可用郵件庫之一來處理郵件發送。 SwiftMailer或梨形郵件是最常用的之一。
4

您必須修改php.ini:

[mail function] 
; 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 = [email protected] 

如果本地主機設置,因爲這個原因改變你的郵件服務器將無法正常工作。

+0

你能指定對於Ubuntu或Linux用戶的東西? –

0

嘗試設置下面在php.ini的東西,

  1. 「SMTP」 到 「mail.YourDomain.com」
  2. 「SMTP_PORT」 到 「25」

或者你可以使用php腳本設置此選項,

//請指定您的郵件服務器或您要使用的其他郵件服務器(例如Gmail,雅虎)

ini_set("SMTP","mail.YourDomain.com"); 

//請指定一個SMTP號碼25和8889是有效的SMTP端口。

ini_set("smtp_port","25"); 
3

默認情況下,您不會安裝SMTP服務器,因此您無法直接從本地主機發送電子郵件。您可以在本地設置SMTP服務器,也可以使用第三方SMTP服務器。 看看http://www.mittalpatel.co.in/php_send_mail_from_localhost_using_gmail_smtp可以讓你瞭解如何使用第三方SMTP服務器從本地主機發送郵件。

+1

此評論中的鏈接幫助我在Mac上本地設置了我的smtp。謝謝! – surfbird0713

+0

@ surfbird0713很高興知道它幫助你! :-) –

0

下面是給我的答案的鏈接:

安裝「假sendmail的窗戶」。如果你不使用XAMPP 你可以在這裏下載:http://glob.com.au/sendmail/sendmail.zip

修改php.ini文件使用它(註釋掉其他線路):

郵件功能

只用於Win32。

SMTP = smtp.gmail.com 

smtp_port = 25 

僅適用於Win32。

sendmail_from = <e-mail username>@gmail.com 

僅適用於Unix。你可以提供參數以及

(default: "sendmail -t -i"). 

sendmail_path = "C:\xampp\sendmail\sendmail.exe -t" 

(忽略「僅針對Unix」一下,因爲我們實際上是在使用sendmail)

你就必須配置「sendmail.ini」文件的目錄中的sendmail安裝:

sendmail的

smtp_server=smtp.gmail.com 
smtp_port=25 
error_logfile=error.log 
debug_logfile=debug.log 
auth_username=<username> 
auth_password=<password> 
force_sender=<e-mail username>@gmail.com 
0

我有這個問題在過去的幾周,我的CentOS的盒子,分享這一對他人也有在PHP本身不具有郵件的問題() nding ...這解決了我的所有郵件()PHP腳本的問題。

// Enable the sendmail in selinux 
setsebool -P httpd_can_sendmail 1 

// Add the following to /etc/postfix/main.cf 
relayhost = smtp.server.com 

// Then from command line 
service postfix restart 
相關問題