ink
ink

Reputation: 223

wp_mail - not working when $headers are set

In my files the wp_mail function does not send a mail if I set the optional $headers parameter. I just don't receive any mail when it's set. If I leave it away, however, the mail arrives.

$headers = 'From: My Name <[email protected]>' . "\r\n";
wp_mail($to, $subjects, $message, $headers); // not working
wp_mail($to, $subjects, $message ); // working

What could cause this?

Upvotes: -1

Views: 3836

Answers (3)

Md Masud
Md Masud

Reputation: 2713

It needs to match the from address domain to from-email and reply-to email addresses.

Here is a complete example with headers, attachments.

$from_name = 'name';
$from = '[email protected]';
$subject = 'test';    
$body = 'Test';
$to = '[email protected]';
$bcc = '[email protected]';
$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: 0

Anonymous
Anonymous

Reputation: 12017

From your answer to my question:

Are you using that exact From: email to test? – Anonymous
Actually I was ... – ink

It seems that your From: address doesn't match the domain you're sending the email from. The mail server you're sending the email to likely rejected the email when it saw that you were trying to spoof the sender address.

Upvotes: 4

visevo
visevo

Reputation: 861

Drop the $header from wp_mail() and add this to your functions.php file:

add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');

function new_mail_from($old) {
    return '[email protected]';
}
function new_mail_from_name($old) {
    return 'Your Blog Name';
}

source:
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_from and https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_from_name

Upvotes: 0

Related Questions