admin管理员组文章数量:1303519
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 <[email protected]>";
$to = "[email protected]";
$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
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 <[email protected]>";
$to = "[email protected]";
$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
Share Improve this question edited Mar 17, 2021 at 8:20 Thando Hlophe asked Mar 15, 2021 at 12:38 Thando HlopheThando Hlophe 16 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 1The 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 <[email protected]>";
$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 "[email protected]";
});
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';
});
本文标签: phpmailerwpmail not sending email on custom function
版权声明:本文标题:phpmailer - wp_mail not sending email on custom function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741674450a2391797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$headers = "From:" . $from . PHP_EOL;
value is getting lost because you're overwriting $headers with an array that just contains the Content-Type. – Rup Commented Mar 15, 2021 at 14:34