WordPress在wp_mail中设置了错误的标题

时间:2019-12-17 作者:Akan Udosen

提交时,我正在wordpress上使用ajax发送一封联系电子邮件。从下面的代码中,您可以看到From标头已正确设置,但wordpress正在从phpmailer\\u init action中选择要设置为标头的标头。有人能帮我解决这个问题吗

add_action(\'wp_ajax_contact_form\', \'contact\');
add_action(\'wp_ajax_nopriv_contact_form\', \'contact\');
function contact(){
  // if ($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {
  /* sanitize and validate */
    $name = isset($_POST[\'name\'])? sanitize_text_field( $_POST[\'name\'] ) : null;
    $phone = isset($_POST[\'phone\'])? sanitize_text_field($_POST[\'phone\']): null;
    $email = isset($_POST[\'email\'])? sanitize_email( $_POST[\'email\'] ) : null;
    $message = isset($_POST[\'message\'])? sanitize_text_field( $_POST[\'message\'] ) : null;
    $subject = isset($_POST[\'subject\'])? sanitize_text_field( $_POST[\'subject\'] ) : null;

    /* start sending */
    $to = \'[email protected]\';
    $headers = array(\'Content-Type: text/html; charset=UTF-8\', "From: $name <$email>");
    // $headers[] = "From: $name <$email>";
    $body = "Phone number: $phone <br><br> $message";
    $sent = wp_mail($to, $subject, $body, $headers);
    if($sent){
      echo "Email delivered successfuly";
    }
  // }
}

1 个回复
SO网友:TomC

您需要向数组中添加换行符,如:

$headers = array(\'Content-Type: text/html; charset=UTF-8\\r\\n\', "From: $name <$email>\\r\\n");

相关推荐

如果固定链接中没有index.php,我会在整个站点中获得404

正如标题所说,当我在管理中更改永久链接时,我的所有页面都返回404(主页除外)。我必须包括在内index.php 作为所有不同路径的起点。如果我包括在内,所有的路径都很好index.php.例如:自定义结构:/index.php/%postname%/%day%/ 将在我浏览我的网站时工作。自定义结构:/%postname%/%day%/ 将不工作(主页除外)。服务器上已启用mod\\u rewrite:$ sudo a2enmod rewrite Module rewrite already e