0

I am creating a contact form on WordPress site.

The form is perfectly working with following code:

//validate email if(!filter_var($email, FILTER_VALIDATE_EMAIL)) my_contact_form_generate_response("error", $email_invalid); else //email is valid { //validate presence of name and message if(empty($name) || empty($topic) || empty($message)){ my_contact_form_generate_response("error", $missing_content); } else //ready to go! { $sent = wp_mail($to, $subject, strip_tags($message), $headers); if($sent) my_contact_form_generate_response("success", $message_sent); //message sent! else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent } } } } else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content); ?>

However, this sends email with 'From' name as 'WordPress'.

I want to customize 'WordPress' with sender name for which I am using $name variable, so that I got to know who send me email without opening it.

Any help will be appreciated.

2 Answers 2

0
$to = '[email protected]';
$name= 'jhon';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('From: '.$name.'');

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

I hope it'll work as you want.

2
  • Thanks, please share me, where to add this code in the snippet I shared in original post.
    – KA.MVP
    Aug 27, 2019 at 5:48
  • This doesn't works. I replaced it with this code: $to = get_option('admin_email'); $subject = "[Contact Form] ".$name; $headers = 'From: '. $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
    – KA.MVP
    Aug 27, 2019 at 5:56
0

The simplest way to change this for ALL outbound mail generated by your site is a plugin called CB Change Mail Sender. I have used it a lot. Performance is not an issue.

1
  • Thanks for your response but I don't want to additional plugins.
    – KA.MVP
    Aug 28, 2019 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.