2013-05-09 43 views
1

我已經看到了使用類似的代碼所有的地方,但我不能得到它的工作...如何發送電子郵件在PHP與wp_mail

$to = '[email protected]'; 
$subject = 'test'; 
$message = 'test'; 
$headers = 'just a test'; 
wp_mail($to, $subject, $message, $headers); 

我也試圖與:

if (wp_mail($to, $subject, $message)) { 
    echo "success"; 
} else { 
    echo "fail"; 
} 

沒有打印。 我錯過了什麼?

我不希望爲此使用插件。

+1

我假設你在服務器上有正確的SMTP設置?或者你能否確認電子郵件來自同一臺服務器? – lampwins 2013-05-09 22:45:12

+1

add'error_reporting(E_ALL); ini_set('display_errors',1);'到腳本的頂部。空白頁面可能意味着PHP遇到了致命錯誤,但服務器處於生產模式並且不會顯示任何內容。您還可以檢查error_log文件中的任何消息。 – drew010 2013-05-10 00:13:20

+0

你的$標題參數看起來不對。 – Gowri 2014-02-08 10:46:44

回答

1

這是我怎麼也得稍微不同:

try { 
    $sent = @wp_mail($to, $subject, $message); 
} catch (Exception $e) { 
    error_log('oops: ' . $e->getMessage()); // this line is for testing only! 
} 

if ($sent) { 
    error_log('hooray! email sent!'); // so is this one 
} else { 
    error_log('oh. email not sent.'); // and this one, too 
} 

哦,它會localhost上不正確的設置不工作。

+1

'wp_mail'不會拋出任何異常,所以try/catch塊是不必要的。 – drew010 2013-05-10 00:12:23

+0

@ drew010謝謝,不知道。很高興我發佈了這個! – montrealist 2013-05-10 12:55:41

+0

使用錯誤抑制並不是一個好習慣:除去'@',特別是因爲你有一個圍繞'wp_mail'調用的try-catch塊。 – Philipp 2015-02-26 12:47:47