Send Email Through SMTP
Routes all WordPress email through an SMTP server, using credentials stored as constants in wp-config.php rather than in the database.
Opens WPIDE on
Code
add_action( 'phpmailer_init', function ( $phpmailer ) {
if ( ! defined( 'SMTP_HOST' ) || '' === SMTP_HOST ) {
return;
}
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->Port = defined( 'SMTP_PORT' ) ? (int) SMTP_PORT : 587;
$phpmailer->SMTPSecure = defined( 'SMTP_SECURE' ) ? SMTP_SECURE : 'tls';
if ( defined( 'SMTP_USER' ) && defined( 'SMTP_PASS' ) ) {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
}
if ( defined( 'SMTP_FROM' ) && is_email( SMTP_FROM ) ) {
$phpmailer->setFrom( SMTP_FROM, wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), false );
}
} );
Notes
Add to wp-config.php: define( 'SMTP_HOST', 'smtp.example.com' ); define( 'SMTP_PORT', 587 ); define( 'SMTP_SECURE', 'tls' ); define( 'SMTP_USER', 'user' ); define( 'SMTP_PASS', 'password' ); and optionally define( 'SMTP_FROM', '[email protected]' ); The snippet does nothing until SMTP_HOST is defined.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.