2013-05-09 76 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