snappymail/plugins/auto-domain-grab/index.php

74 lines
2.2 KiB
PHP
Raw Normal View History

2015-11-19 01:25:36 +08:00
<?php
2015-11-19 01:25:36 +08:00
/**
* This extension automatically detects the IMAP and SMTP settings by
* extracting them from the email address itself. For example, if the user
2022-02-03 19:47:32 +08:00
* 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:
* 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
{
const
NAME = 'Auto Domain Selection',
2022-02-03 19:53:16 +08:00
VERSION = '2.8',
REQUIRED = '2.8.0',
CATEGORY = 'General',
DESCRIPTION = 'Sets the IMAP/SMTP host based on the user\'s login';
2020-03-11 21:17:52 +08:00
2022-02-03 19:47:32 +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
{
2022-02-03 19:47:32 +08:00
$this->addHook('smtp.before-connect', 'FilterSmtpCredentials');
$this->addHook('imap.before-connect', 'FilterImapCredentials');
}
/**
2022-02-03 19:47:32 +08:00
* This function detects the IMAP Host, and if it is set to 'auto', replaces it with the MX or email domain.
*/
2022-02-03 19:47:32 +08:00
public function FilterImapCredentials(\RainLoop\Model\Account $oAccount, \MailSo\Imap\ImapClient $oImapClient, array &$aImapCredentials)
{
2022-02-03 19:47:32 +08:00
// Check for mail.$DOMAIN as entered value in RL settings
if (!empty($aImapCredentials['Host']) && 'auto' === $aImapCredentials['Host'])
{
2022-02-03 19:47:32 +08:00
$domain = \substr(\strrchr($oAccount->Email(), '@'), 1);
$mxhosts = array();
if (\getmxrr($domain, $mxhosts) && $mxhosts)
{
2022-02-03 19:47:32 +08:00
$aImapCredentials['Host'] = $mxhosts[0];
}
else
{
$aImapCredentials['Host'] = $this->imap_prefix.$domain;
}
}
}
/**
2022-02-03 19:47:32 +08:00
* This function detects the SMTP Host, and if it is set to 'auto', replaces it with the MX or email domain.
*/
2022-02-03 19:47:32 +08:00
public function FilterSmtpCredentials(\RainLoop\Model\Account $oAccount, \MailSo\Smtp\SmtpClient $oSmtpClient, array &$aSmtpCredentials)
{
2022-02-03 19:47:32 +08:00
// Check for mail.$DOMAIN as entered value in RL settings
if (!empty($aSmtpCredentials['Host']) && 'auto' === $aSmtpCredentials['Host'])
{
2022-02-03 19:47:32 +08:00
$domain = \substr(\strrchr($oAccount->Email(), '@'), 1);
$mxhosts = array();
if (\getmxrr($domain, $mxhosts) && $mxhosts)
{
$aSmtpCredentials['Host'] = $mxhosts[0];
}
else
{
2022-02-03 19:47:32 +08:00
$aSmtpCredentials['Host'] = $this->smtp_prefix . $domain;
}
}
}
2015-11-19 01:25:36 +08:00
}