Skip to content

Instantly share code, notes, and snippets.

@kaelri
Created June 29, 2021 16:22
Show Gist options
  • Select an option

  • Save kaelri/614e50f8beaa62f0f0e55a729602ea04 to your computer and use it in GitHub Desktop.

Select an option

Save kaelri/614e50f8beaa62f0f0e55a729602ea04 to your computer and use it in GitHub Desktop.
Make WordPress use an SMTP server for all outgoing email notifications.
<?php
class mdgSMTP {
public static function setup() {
add_action( 'phpmailer_init', [ __CLASS__, 'configure_php_mailer' ] );
if ( class_exists('WP_CLI') ) {
WP_CLI::add_command( 'send-test-email', [ __CLASS__, 'send_test_email' ] );
}
}
public static function configure_php_mailer( $phpmailer ){
if ( !defined('SMTP_USERNAME') || !defined('SMTP_PASSWORD') ) return;
// MESSAGE
$phpmailer->FromName = '…';
$phpmailer->From = '…@…';
// SERVER
$phpmailer->isSMTP();
$phpmailer->Host = '…';
$phpmailer->Port = 2525; // WP Engine does not allow port 25. Allowed ports: 2525 (recommended), 587 (required for Microsoft), or 465.
// USER
$phpmailer->SMTPAuth = true;
$phpmailer->Username = SMTP_USERNAME; // Set these constants in wp-config.php, e.g.: define('SMTP_USERNAME', '…');
$phpmailer->Password = SMTP_PASSWORD; // Set these constants in wp-config.php, e.g.: define('SMTP_PASSWORD', '…');
// SECURITY
$phpmailer->SMTPAutoTLS = true;
}
public static function send_test_email( $args ) {
$email = $args[0];
WP_CLI::line( 'Sending test email to: ' . $email );
wp_mail(
$email,
'Testing SMTP Email from ' . get_bloginfo('name'),
'This email should have been sent from WordPress via the configured SMTP server.'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment