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
1 Answer
Reset to default 0You 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
评论列表(0条)