1

When using this code below with wp_mail() I always get in the header [email protected]. And in Thunderbird in column Correspondents: Wordpress. But I need: 'From: "Klantenservice"<' . $emailTo . '>'

In https://developer.wordpress.org/reference/functions/wp_mail/ and other pages I can not find a solution for this.

    $body  = __('Name:', 'Avada')." $user_name \n\n";
    $body .= __('Email:', 'Avada')." $user_email \n\n";
    $body .= __('Telefoon:', 'Avada')." $telephone \n\n";
    $body .= __('Betreft:', 'Avada')." $subject \n\n";
    $body .= __('Bericht:', 'Avada')."\n $message";

    $headers = 'From: "Klantenservice"<' . $emailTo . '>' . "\r\n";
    $emailTo = '[email protected]';
    //$mail = wp_mail($user_email, $subject, $body, $headers);
    $mail = wp_mail($emailTo, 'Contact verzoek', $body, '');

How can I get this correct?

Here the solution add this in functions.php:

add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name )
{
return "My Name";
}
3
  • why do you set the from headerto the $emailTo value? what is the value at all (not sure it relevant but maybe) Commented Oct 3, 2016 at 17:29
  • 5
    You never pass $headers to function in the code you posted?
    – Rarst
    Commented Oct 3, 2016 at 17:30
  • 2
    Please post your answer as an answer to keep the site format tidy. You can accept it as correct too, after a couple of days. Commented Oct 3, 2016 at 18:04

2 Answers 2

2

It looks like you are setting the $emailto variable after the place you are using it.

0

The problem is two-fold. First, setting the $emailTo after the header value where that variable is used (so it's empty in the header).

Second, the $header value that is set is not used in the actual call to wp_mail(). Instead it passes an empty value. (It is set in the line before, but it is commented out, presumably for testing.)

Here's the problematic section:

$headers = 'From: "Klantenservice"<' . $emailTo . '>' . "\r\n";
$emailTo = '[email protected]';
//$mail = wp_mail($user_email, $subject, $body, $headers);
$mail = wp_mail($emailTo, 'Contact verzoek', $body, '');

Here's what it should be:

$emailTo = '[email protected]';
$headers = 'From: "Klantenservice"<' . $emailTo . '>' . "\r\n";
//$mail = wp_mail($user_email, $subject, $body, $headers);
$mail = wp_mail($emailTo, 'Contact verzoek', $body, $headers);

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.