Created
June 29, 2021 16:22
-
-
Save kaelri/614e50f8beaa62f0f0e55a729602ea04 to your computer and use it in GitHub Desktop.
Make WordPress use an SMTP server for all outgoing email notifications.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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