Shankar
Shankar

Reputation: 29

How to add email address in header (from field) in wp_mail

I have created contact us form for users where user can add their email address and name. I wish to add these name and email address field in wp_mail headers. I am using following code.
$headers = 'From: My Name <[email protected]>' . "\r\n";
wp_mail($to, $subjects, $message, $headers); // not working
wp_mail($to, $subjects, $message ); // working

I think this happening is because my From: address doesn't match to the domain i'm sending the email from. But is there any way for me to accomplish above using wp_mail.

I am getting following debugging information using smtp debug
2017-02-12 13:48:40 CLIENT -> SERVER: QUIT
2017-02-12 13:48:40 SMTP -> get_lines(): $data is ""
2017-02-12 13:48:40 SMTP -> get_lines(): $str is "* ..* Service closing transmission channel
" 2017-02-12 13:48:40 SERVER -> CLIENT: *
..* Service closing transmission channel
2017-02-12 13:48:40 Connection: closed

Upvotes: 1

Views: 6924

Answers (1)

I&#39;m Joe Too
I&#39;m Joe Too

Reputation: 5860

Try this:

$headers = array(
  'From: My Name <[email protected]>'
);
$headers = implode( PHP_EOL, $headers );
wp_mail( $to, $subjects, $message, $headers );

PHP_EOL will add the proper line break based on the system it's on (where \r\n is for Windows, \n is for Unix). Using implode() will make sure it's only added if needed. In this case, you're only sending one header so the line break isn't neaded anyways. But if you want to send more headers:

$headers = array(
  'Bcc: [email protected]',
  'From: My Name <[email protected]>',
  'Reply-To: [email protected]'
);
$headers = implode( PHP_EOL, $headers );
wp_mail( $to, $subjects, $message, $headers );

Upvotes: 2

Related Questions