From ef61603431be51923e9449f5c43df797ccc0e878 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Thu, 3 Feb 2022 12:47:32 +0100 Subject: [PATCH] Resolve #207 --- plugins/auto-domain-grab/index.php | 74 +++++++++++++----------------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/plugins/auto-domain-grab/index.php b/plugins/auto-domain-grab/index.php index 34fc0c3ca..3f5895d41 100644 --- a/plugins/auto-domain-grab/index.php +++ b/plugins/auto-domain-grab/index.php @@ -3,8 +3,8 @@ /** * This extension automatically detects the IMAP and SMTP settings by * extracting them from the email address itself. For example, if the user - * attemps to login as "info@example.com", then the IMAP and SMTP host would - * be set to to "example.com". + * attemps to login as 'info@example.com', then the IMAP and SMTP host would + * be set to to 'example.com'. * * Based on: * https://github.com/the-djmaze/snappymail/blob/master/plugins/override-smtp-credentials/index.php @@ -15,70 +15,58 @@ class AutoDomainGrabPlugin extends \RainLoop\Plugins\AbstractPlugin { const NAME = 'Auto Domain Selection', - VERSION = '2.1', + VERSION = '2.1', REQUIRED = '2.5.0', CATEGORY = 'General', DESCRIPTION = 'Sets the IMAP/SMTP host based on the user\'s login'; - private $imap_prefix = "mail."; - private $smtp_prefix = "mail."; + private $imap_prefix = 'mail.'; + private $smtp_prefix = 'mail.'; public function Init() : void { - $this->addHook('smtp.credentials', 'FilterSmtpCredentials'); - $this->addHook('imap.credentials', 'FilterImapCredentials'); + $this->addHook('smtp.before-connect', 'FilterSmtpCredentials'); + $this->addHook('imap.before-connect', 'FilterImapCredentials'); } /** - * This function detects the IMAP Host, and if it is set to "auto", replaces it with the MX or email domain. - * - * @param \RainLoop\Model\Account $oAccount - * @param array $aImapCredentials + * This function detects the IMAP Host, and if it is set to 'auto', replaces it with the MX or email domain. */ - public function FilterImapCredentials($oAccount, &$aImapCredentials) + public function FilterImapCredentials(\RainLoop\Model\Account $oAccount, \MailSo\Imap\ImapClient $oImapClient, array &$aImapCredentials) { - if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aImapCredentials)) + // Check for mail.$DOMAIN as entered value in RL settings + if (!empty($aImapCredentials['Host']) && 'auto' === $aImapCredentials['Host']) { - // Check for mail.$DOMAIN as entered value in RL settings - if (!empty($aImapCredentials['Host']) && 'auto' === $aImapCredentials['Host']) + $domain = \substr(\strrchr($oAccount->Email(), '@'), 1); + $mxhosts = array(); + if (\getmxrr($domain, $mxhosts) && $mxhosts) { - $domain = substr(strrchr($oAccount->Email(), "@"), 1); - $mxhosts = array(); - if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0) - { - $aImapCredentials['Host'] = $mxhosts[0]; - } - else - { - $aImapCredentials['Host'] = $this->imap_prefix.$domain; - } + $aImapCredentials['Host'] = $mxhosts[0]; + } + else + { + $aImapCredentials['Host'] = $this->imap_prefix.$domain; } } } /** - * This function detects the SMTP Host, and if it is set to "auto", replaces it with the MX or email domain. - * - * @param \RainLoop\Model\Account $oAccount - * @param array $aSmtpCredentials + * This function detects the SMTP Host, and if it is set to 'auto', replaces it with the MX or email domain. */ - public function FilterSmtpCredentials($oAccount, &$aSmtpCredentials) + public function FilterSmtpCredentials(\RainLoop\Model\Account $oAccount, \MailSo\Smtp\SmtpClient $oSmtpClient, array &$aSmtpCredentials) { - if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aSmtpCredentials)) + // Check for mail.$DOMAIN as entered value in RL settings + if (!empty($aSmtpCredentials['Host']) && 'auto' === $aSmtpCredentials['Host']) { - // Check for mail.$DOMAIN as entered value in RL settings - if (!empty($aSmtpCredentials['Host']) && 'auto' === $aSmtpCredentials['Host']) + $domain = \substr(\strrchr($oAccount->Email(), '@'), 1); + $mxhosts = array(); + if (\getmxrr($domain, $mxhosts) && $mxhosts) { - $domain = substr(strrchr($oAccount->Email(), "@"), 1); - $mxhosts = array(); - if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0) - { - $aSmtpCredentials['Host'] = $mxhosts[0]; - } - else - { - $aSmtpCredentials['Host'] = $this->smtp_prefix.$domain; - } + $aSmtpCredentials['Host'] = $mxhosts[0]; + } + else + { + $aSmtpCredentials['Host'] = $this->smtp_prefix . $domain; } } }