I add the following code to the functions.php
file.
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
and wp-config.php
// SMTP email settings
define( 'SMTP_USER', '{email}' );
define( 'SMTP_PASS', '{password}' );
define( 'SMTP_HOST', '{server}' );
define( 'SMTP_FROM', '{from}' );
define( 'SMTP_NAME', '{name}' );
define( 'SMTP_PORT', '465' );
define( 'SMTP_SECURE', 'ssl' );
define( 'SMTP_AUTH', true );
"wp-login.php?action=lostpassword
" when I go here, I enter my e-mail address and click the send button,
"Failed to send e-mail. Your site may not be properly configured to send e-mail." I am getting this error.
But when I paste the same code into another WordPress with WooCommerce plugin installed, I forgot my WooCommerce password. Mail can be successfully sent from the front-end design.
Where is the problem? Am I doing something wrong?
3rd step
add_action( 'wp_mail_failed', function ( $error ) {
error_log( $error->get_error_message() );
} );
Not working (IDK). But I searched and found the code below,
add_action('wp_mail_failed', 'log_mailer_errors', 10, 1);
function log_mailer_errors( $wp_error ){
$fn = ABSPATH . '/mail.log'; // say you've got a mail.log file in your server root
$fp = fopen($fn, 'a');
fputs($fp, "Mailer Error: " . $wp_error->get_error_message() ."\n");
fclose($fp);
}
This gave me the following error
Mailer Error: Invalid address: (From): wordpress@localhost
I found the relevant location from pluggable.php
.
Since I was working on localhost, it was emitting an invalid e-mail address because it did not create a valid TLD.
if ( ! isset( $from_email ) ) {
// Get the site domain and get rid of www.
$sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
if ( 'www.' === substr( $sitename, 0, 4 ) ) {
$sitename = substr( $sitename, 4 );
}
$from_email = 'wordpress@' . $sitename;
}
This means, "$phpmailer->From = SMTP_FROM;
" instead of "add_filter ('wp_mail_from', 'set_from');
" should use.
function set_from()
{
return '[email protected]';
}
add_filter('wp_mail_from', 'set_from');
Works now.
I don't understand why
phpmailer_init
doesn't overwrite from information.
By the way, the sender still appears to be what I defined in ($phpmailer-> From
).
I guess this is some kind of bug and should be reported to WordPress?