options = get_option( self::OPTION_NAME, [] ); $this->init(); } /** * Start the engine running * * @return void */ public function init() { $this->settings = new SMTP_Email_Settings( $this->options); add_action( 'adminoptimizer_add_submenu_page', [ $this, 'add_submenu_page' ] ); add_action( 'phpmailer_init', [ $this, 'phpmailer_smtp_setup' ] ); $from_priority = ! empty( $this->options['force_from'] ) ? PHP_INT_MAX : 1; add_filter( 'wp_mail_from', [ $this, 'default_mail_from' ], $from_priority ); add_filter( 'wp_mail_from_name', [ $this, 'default_mail_from_name' ], $from_priority ); } /** * Add submenu to WP Menu page * * @return void */ public function add_submenu_page() { add_submenu_page( ADMINOPTIMIZER_MODULES_MENU_SLUG, __( 'SMTP Email', 'admin-optimizer' ), __( 'SMTP Email', 'admin-optimizer' ), 'manage_options', self::MENU_SLUG, [ $this->settings, 'render_settings_page' ] ); } /** * Default from-address filter — overridable by other plugins at priority >= 1. * * @param string $from Current from address. * @return string */ public function default_mail_from( $from ) { if ( empty( $this->options['email_from'] ) ) { return $from; } if ( ! empty( $this->options['force_from'] ) ) { return $this->options['email_from']; } // Default mode: only replace the WordPress-generated default; honour explicit From: headers. $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST ); $sitename = preg_replace( '#^www\.#', '', strtolower( $sitename ) ); return ( 'wordpress@' . $sitename === $from ) ? $this->options['email_from'] : $from; } /** * Default from-name filter — overridable by other plugins at priority >= 1. * * @param string $name Current from name. * @return string */ public function default_mail_from_name( $name ) { if ( empty( $this->options['send_from'] ) ) { return $name; } if ( ! empty( $this->options['force_from'] ) ) { return $this->options['send_from']; } // Default mode: only replace the WordPress-generated default; honour explicit From: headers. return ( 'WordPress' === $name ) ? $this->options['send_from'] : $name; } /** * Phpmailer smtp setup * * @param \PHPMailer $phpmailer phpmailer class. * * @return void */ public function phpmailer_smtp_setup( $phpmailer ) { if ( ! empty( $this->options ) ) { $phpmailer->isSMTP(); if ( isset( $this->options['smtp_server'] ) ) { $phpmailer->Host = $this->options['smtp_server']; } // For SES, always derive the host from the stored region so it stays correct if the region changes. if ( isset( $this->options['smtp_provider'], $this->options['smtp_ses_region'] ) && 'ses' === $this->options['smtp_provider'] ) { $phpmailer->Host = 'email-smtp.' . sanitize_key( $this->options['smtp_ses_region'] ) . '.amazonaws.com'; } if ( isset( $this->options['smtp_auth'] ) ) { $phpmailer->SMTPAuth = true; } if ( isset( $this->options['smtp_port'] ) ) { $phpmailer->Port = $this->options['smtp_port']; } if ( isset( $this->options['smtp_username'] ) ) { $phpmailer->Username = $this->options['smtp_username']; } if ( isset( $this->options['smtp_password'] ) ) { $phpmailer->Password = self::decrypt_password( $this->options['smtp_password'] ); } if ( isset( $this->options['smtp_secure'] ) ) { $phpmailer->SMTPSecure = $this->options['smtp_secure']; } if ( ! empty( $this->options['email_from'] ) && empty( $phpmailer->Sender ) ) { $phpmailer->Sender = $this->options['email_from']; } } } }