2015-11-19 01:25:36 +08:00
|
|
|
<?php
|
2015-11-19 04:22:39 +08:00
|
|
|
|
2015-11-19 01:25:36 +08:00
|
|
|
/**
|
2021-08-28 05:49:03 +08:00
|
|
|
* 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".
|
2015-11-19 01:25:36 +08:00
|
|
|
*
|
|
|
|
* Based on:
|
2020-10-15 22:21:52 +08:00
|
|
|
* https://github.com/the-djmaze/snappymail/blob/master/plugins/override-smtp-credentials/index.php
|
2020-03-11 21:17:52 +08:00
|
|
|
*
|
2015-11-19 01:25:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
class AutoDomainGrabPlugin extends \RainLoop\Plugins\AbstractPlugin
|
|
|
|
{
|
2021-02-10 16:50:20 +08:00
|
|
|
const
|
2021-08-28 05:49:03 +08:00
|
|
|
NAME = 'Auto Domain Selection',
|
2021-04-21 16:34:54 +08:00
|
|
|
VERSION = '2.1',
|
|
|
|
REQUIRED = '2.5.0',
|
2021-02-10 16:50:20 +08:00
|
|
|
CATEGORY = 'General',
|
2021-08-28 05:49:03 +08:00
|
|
|
DESCRIPTION = 'Sets the IMAP/SMTP host based on the user\'s login';
|
2020-03-11 21:17:52 +08:00
|
|
|
|
2018-02-03 20:01:50 +08:00
|
|
|
private $imap_prefix = "mail.";
|
|
|
|
private $smtp_prefix = "mail.";
|
2020-03-11 21:17:52 +08:00
|
|
|
|
2020-08-31 00:04:54 +08:00
|
|
|
public function Init() : void
|
2015-11-19 04:22:39 +08:00
|
|
|
{
|
2021-04-14 20:30:42 +08:00
|
|
|
$this->addHook('smtp.credentials', 'FilterSmtpCredentials');
|
|
|
|
$this->addHook('imap.credentials', 'FilterImapCredentials');
|
2015-11-19 04:22:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-30 01:26:22 +08:00
|
|
|
* This function detects the IMAP Host, and if it is set to "auto", replaces it with the MX or email domain.
|
2015-11-19 04:22:39 +08:00
|
|
|
*
|
|
|
|
* @param \RainLoop\Model\Account $oAccount
|
|
|
|
* @param array $aImapCredentials
|
|
|
|
*/
|
|
|
|
public function FilterImapCredentials($oAccount, &$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'])
|
|
|
|
{
|
2018-02-03 20:01:50 +08:00
|
|
|
$domain = substr(strrchr($oAccount->Email(), "@"), 1);
|
2018-11-30 01:00:25 +08:00
|
|
|
$mxhosts = array();
|
|
|
|
if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0)
|
|
|
|
{
|
|
|
|
$aImapCredentials['Host'] = $mxhosts[0];
|
|
|
|
}
|
2020-03-11 21:17:52 +08:00
|
|
|
else
|
2018-11-30 01:00:25 +08:00
|
|
|
{
|
|
|
|
$aImapCredentials['Host'] = $this->imap_prefix.$domain;
|
|
|
|
}
|
2015-11-19 04:22:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-30 01:26:22 +08:00
|
|
|
* This function detects the SMTP Host, and if it is set to "auto", replaces it with the MX or email domain.
|
2015-11-19 04:22:39 +08:00
|
|
|
*
|
|
|
|
* @param \RainLoop\Model\Account $oAccount
|
|
|
|
* @param array $aSmtpCredentials
|
|
|
|
*/
|
|
|
|
public function FilterSmtpCredentials($oAccount, &$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'])
|
|
|
|
{
|
2018-02-03 20:01:50 +08:00
|
|
|
$domain = substr(strrchr($oAccount->Email(), "@"), 1);
|
2018-11-30 01:00:25 +08:00
|
|
|
$mxhosts = array();
|
|
|
|
if(getmxrr($domain, $mxhosts) && sizeof($mxhosts) > 0)
|
|
|
|
{
|
|
|
|
$aSmtpCredentials['Host'] = $mxhosts[0];
|
2020-03-11 21:17:52 +08:00
|
|
|
}
|
|
|
|
else
|
2018-11-30 01:00:25 +08:00
|
|
|
{
|
|
|
|
$aSmtpCredentials['Host'] = $this->smtp_prefix.$domain;
|
|
|
|
}
|
2015-11-19 04:22:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 01:25:36 +08:00
|
|
|
}
|