php - Wordpress is setting from header wrongly in wp_mail

I am sending a contact email using ajax on wordpress on submit. From the code below you can see that the FROM header is

I am sending a contact email using ajax on wordpress on submit. From the code below you can see that the FROM header is correctly set but instead wordpress is picking the header from phpmailer_init action to set as the header. can anyone help me resolve this

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";
    }
  // }
}

I am sending a contact email using ajax on wordpress on submit. From the code below you can see that the FROM header is correctly set but instead wordpress is picking the header from phpmailer_init action to set as the header. can anyone help me resolve this

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";
    }
  // }
}
Share Improve this question asked Dec 17, 2019 at 10:22 Akan UdosenAkan Udosen 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to add newlines to your array like:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744897180a4599765.html

相关推荐

  • php - Wordpress is setting from header wrongly in wp_mail

    I am sending a contact email using ajax on wordpress on submit. From the code below you can see that the FROM header is

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信