Wordpress没有收到联系表格电子邮件

时间:2015-04-10 18:32:07

标签: php wordpress forms email

我有一个网站,我正在处理一个联系表格。简单名称,电子邮件和消息字段。但是,我没有收到表格中的电子邮件;虽然我确实收到了#34; Message Sent"成功选择。

我找到了一种测试wp_mail的方法,看看是不是这样,使用https://gist.github.com/butlerblog/5c9b805529c419b81447#file-test_wp_mail-php并且运行正常 - 所以我不知道问题是什么。

<?php

//loading wordpress functions
    require( '../../../wp-load.php' );


define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');


 $to = get_option('admin_email');                  //Enter your e-mail here.
 $subject =  get_theme_option(tk_theme_name.'_contact_contact_subject');

 $from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');

 $body = "You received e-mail from ".$from."  [".$name."] "." using ".$sitename."\n\n\n".$message;

 $send = wp_mail($to, $subject, $body, $headers) ;


if($send){
wp_redirect($return.'?sent=success');
}else{
    wp_redirect($return.'?sent=error');

    }

 ?> 

正如我所说,确实如此。

wp_redirect($return.'?sent=success')

所以我不知道出了什么问题。

修改

变量转储

To: uriah.h.brown@gmail.com
Subject: E-Mail from HollyBrown.Net
From: TestName
Name: test@yahoo.com
Message: TestMessage
Headers: From: test@yahoo.com Reply-To: E-Mail from HollyBrown.Net
Return: http://www.hollybrown.net/contact/
Sitename: The Artwork of Holly Brown
Body: You received e-mail from TestName [test@yahoo.com] using The Artwork of Holly Brown TestMessage
Send: 1

Warning: Cannot modify header information - headers already sent by (output started at /home/panoramicpanda/hollybrown.net/wp-content/themes/widely/sendcontact.php:21) in /home/panoramicpanda/hollybrown.net/wp-includes/pluggable.php on line 1178

1 个答案:

答案 0 :(得分:0)

标头已发送错误导致我相信在您尝试发送标头之前已经启动了输出。在这种情况下,重定向不会起作用。试试这个:

    /loading wordpress functions
    require( '../../../wp-load.php' );

ob_start(); // Starts data buffer.
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');


 $to = get_option('admin_email');                  //Enter your e-mail here.
 $subject =  get_theme_option(tk_theme_name.'_contact_contact_subject');

 $from = $_POST['contactname'];
$name = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $name <$from>\n";
$headers .= "Reply-To: $subject <$from>\n";
$return = $_POST['returnurl'];
$sitename =get_bloginfo('name');

 $body = "You received e-mail from ".$from."  [".$name."] "." using ".$sitename."\n\n\n".$message;

 $send = wp_mail($to, $subject, $body, $headers) ;
 ob_end_clean();// Purges data in buffer. 

if($send){
wp_redirect($return.'?sent=success');
}else{
    wp_redirect($return.'?sent=error');

    }

注意ob_start和ob_end_clean。这会将您的输出输入&#34;缓冲区&#34;并输出干净。这是一个问题的原因,是因为如果在您的sendmail之前发送任何标记(即:通过模板等生成的html,那么标题已经启动。此时,除非您的服务器配置明确允许您修改已发送的标头,然后你会得到你得到的错误。使用输出缓冲允许你按照你的意愿发送数据。