0

I have updated my wordpress to the new 5.7 version, I have a function in my functions.php file to send an email.

The thing is, i have used this function before and it all worked well. Now after upgrading, i am unable send email from my custom function send_email()

Custom email function in functions.php

 function send_email(){
        $from = "Admin <no-reply@lamza.co.za>";
        $to = "testing@gmail.com";
        $subject = "Lamza Activation";
        $activationEmail = '';
        $headers = "From:" . $from . PHP_EOL;
        $headers = array('Content-Type: text/html; charset=UTF-8');
        wp_mail($to, $subject, $activationEmail, $headers);
    }

I keep getting false value.

I am able to send test email using Easy WP SMTP but I can not on my function.

I need to know what I need to change, configurations or is it my human error.

Thanks yall

7
  • Can you see any errors in your logs? It's probably worth also implementing the wp_mail_failed hook or temporarily adding an extra log to the two places it's called in wp-includes/pluggable.php. – Rup Mar 15 at 12:49
  • 1
    I think you're also missing the From line from $headers: you're overwriting the value with the content type not adding to it. – Rup Mar 15 at 12:54
  • I added the functions to my functions.php and i am not getting any errors. Before posting this question, i got this error but now i can not reproduce this error display WP_Error Object ( [errors] => Array ( [wp_mail_failed] => Array ( [0] => Invalid address: (From): wordpress@localhost – Thando Hlophe Mar 15 at 13:29
  • Yes, wordpress@localhost is the default if you don't set a from address. Your $headers = "From:" . $from . PHP_EOL; value is getting lost because you're overwriting $headers with an array that just contains the Content-Type. – Rup Mar 15 at 14:34
1

The way you're applying the headers is incorrect. You set a "from" address in the headers as a string, and then you immediately overwrite that with an array (so the "from" address is lost). (You're also setting the $from address twice)

Try it this way:

function send_email(){
   $to = $email;
   $subject = "Lamza Activation";
   $activationEmail = '';
   $from = "testing <no-reply@lamza.co.za>";
   $headers[] = "From:" . $from . PHP_EOL;
   $headers[] = 'Content-Type: text/html; charset=UTF-8';
   wp_mail($to, $subject, $activationEmail, $headers);
}

OR, you could also use the wp_mail filters to set the from address and content type instead.

function send_email(){
   $to = $email;
   $subject = "Lamza Activation";
   $activationEmail = '';
   wp_mail($to, $subject, $activationEmail);
}

add_filter( 'wp_mail_from', function( $from ) {
     return "no-reply@lamza.co.za";
});

add_filter( 'wp_mail_from_name', function( $from_name ) {
     return "testing";
});

add_filter( 'wp_mail_content_type', function( $content_type {
     return 'text/html';
});

// This one you may not need as WP defaults to UTF-8...
add_filter( 'wp_mail_charset', function( $charset ) {
     return 'UTF-8';
});
2
  • I did exactly this and still nothing, I enabled all the error reporting settings but still no errors being displayed. Everytime the wp_mail function fires, the website crashes. My code is a copy and paste from an old project which worked perfectly, now for some odd reason wp_mail is crashing and tried most settings, even deleted my code and replaced it with code from wordpress directly – Thando Hlophe Mar 17 at 9:11
  • My wordpress had been referencing the wrong folder in the plugable.php file in wp-includes. Thank you – Thando Hlophe Mar 17 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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