public9nf
public9nf

Reputation: 1399

Set reply-to address in wp_mail header

I use wp_mail to send notifications out of my WordPress theme. How can i add a reply-to address to the follwing wp_mail script:

$recipient  = "recipient@example.com";
$headers = array('Content-Type: text/html; charset=UTF-8','From: MyWebsite <'mywebsite@example.com'>');
$message = 'Here is the sent message';
        
wp_mail( $recipient, 'Here comes the Subject', $message, $headers );     

   

Upvotes: 8

Views: 6937

Answers (2)

Md Masud
Md Masud

Reputation: 2711

Here is a complete example with headers and attachments.

$from_name = 'name';
$from = 'from@example.test';
$subject = 'test';    
$body = 'Test';
$to = 'test@example.test';
$bcc = 'bccemail@example.text';
$attachments[] = WP_CONTENT_DIR.'/uploads/'.$path_to_file;
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: '.$from_name.' <'.$from.'>';
$headers[] = 'Reply-To: '.$from_name.' <'.$from.'>';
//add BCC if want 
$headers[] = 'Bcc: '.$bcc;

$success = wp_mail($to, $subject, $body, $headers, $attachments);

Upvotes: 1

rank
rank

Reputation: 2544

You can set the reply-to address inside of the $headers array. It needs to have the email address inside of <> and I would suggest using a name to make sure everything works fine.

$headers[] = 'Reply-To: Firstname Lastname <your@mail.com>';

I added a subject for your email. So your code would look like:

$recipient  = "recipient@example.com";
$subject = "Your subject";
$message = "Here is the sent message";
$headers = array(
    'Content-Type: text/html; charset=UTF-8',
    'From: MyWebsite <mywebsite@example.com>',
    'Reply-To: Firstname Lastname <your@mail.com>'
);

wp_mail( $recipient, $subject, $message, $headers );

Upvotes: 12

Related Questions