79250337

Date: 2024-12-04 08:27:49
Score: 0.5
Natty:
Report link

I was also trying to send emails directly via SMTP in WordPress without using any plugins and faced some problems. If anyone else is facing the same issue in 2024, I hope this will help.

Add this two filter to your theme functions.php file

add_filter("wp_mail_from_name", function(){
    return "MAIL_FROM_NAME";
});

add_filter("wp_mail_from", function(){
    return "MAIL_FROM";
});

Then add the phpmailer_init action

add_action('phpmailer_init', function($phpmailer) {
    
    $phpmailer->isSMTP();
    $phpmailer->Host       = 'smtp.gmail.com'; // Replace with your SMTP host (e.g., smtp.gmail.com)
    $phpmailer->SMTPAuth   = true;              // Enable SMTP authentication
    $phpmailer->Username   = '[email protected]'; // SMTP username
    $phpmailer->Password   = 'SMTP_PASSWORD';         // SMTP password
    $phpmailer->SMTPSecure = 'ssl';                   // Encryption: 'ssl' or 'tls'
    $phpmailer->Port       = 465;                     // TCP port to connect to
    $phpmailer->From       = '[email protected]'; // From email
    $phpmailer->FromName   = 'FROM_NAME';             // From name
    $phpmailer->addReplyTo('[email protected]');
    // $phpmailer->SMTPDebug  = 2;
});
Reasons:
  • Whitelisted phrase (-1): hope this will help
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): facing the same issue
  • Low reputation (0.5):
Posted by: Mohammad Salim Hosen