RainLoop / rainloop-webmail

Simple, modern & fast web-based email client
http://rainloop.net
MIT License
4.1k stars 889 forks source link

Auto Detect Server from Email Address #863

Open ghost opened 8 years ago

ghost commented 8 years ago

Hi There,

Is there any chance of setting up RainLoop as a single interface for multiple shared hosting servers (i.e. many clients with many addresses spread over multiple servers). In this case, the server name would need to be read in from the email address automatically,

For example: info@rhysit.co.za password

RainLoop auto detects the server name 'rhysit.co.za', and attempts to log in using 'info@rhysit.co.za' as the username, 'password' as the password, 'rhysit.co.za' as the server.

Thanks Rhys

ghost commented 8 years ago

Take a look at these search results. Not sure if it helps, but maybe setting * as a wildcard, will allow all domains which have an active account.

ghost commented 8 years ago

Hi There!

Thanks for the response. I did try that, however I realised it would only work if we were using one server. Due to the requirement of still having to specify the specific server to manage the wildcard domain, even though all addresses are allowed, they would all be pointed towards one imap server instead of each to their separate server :/

ghost commented 8 years ago

Oh Cool! Just discovered this from one of the links. I'll check it out!

https://github.com/RainLoop/rainloop-webmail/tree/master/plugins/override-smtp-credentials

jas8522 commented 8 years ago

Hey there,

I used that plugin (override-smtp-credentials) to build a custom plugin that does exactly this for IMAP and SMTP. Looking into fixing up the code a bit and possibly adding it to the plugins here on GitHub

rhyswilliamsza commented 8 years ago

Hi Jas!

If you could send that my way, I would really appreciate it. I could possibly take a look and help?

Rhys

jas8522 commented 8 years ago

Hey Rhys,

Here's what it looks like:

/**
 * Based on:
 * https://github.com/RainLoop/rainloop-webmail/blob/master/plugins/override-smtp-credentials/index.php
 */

class OverrideServerAddressesPlugin extends \RainLoop\Plugins\AbstractPlugin
{
    public function Init()
    {
        $this->addHook('filter.smtp-credentials', 'FilterSmtpCredentials');
        $this->addHook('filter.imap-credentials', 'FilterImapCredentials');
    }

    /**
     * @param \RainLoop\Model\Account $oAccount
     * @param array $aSmtpCredentials
     */
    public function FilterSmtpCredentials($oAccount, &$aSmtpCredentials)
    {
        if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aSmtpCredentials))
        {
            $sEmail = $oAccount->Email();

            // Check for mail.$DOMAIN as entered value in RL settings
            if ( $aSmtpCredentials['Host'] == "mail.%DOMAIN" || $aSmtpCredentials['Host'] == "" )
            {
                $aSmtpCredentials['Host'] = "mail." . $this->getDomainFromEmail($sEmail);
            }
        }
    }

    /**
     * @param \RainLoop\Model\Account $oAccount
     * @param array $aImapCredentials
     */
    public function FilterImapCredentials($oAccount, &$aImapCredentials)
    {
        if ($oAccount instanceof \RainLoop\Model\Account && \is_array($aImapCredentials))
                {
            $sEmail = $oAccount->Email();

            // Check for mail.$DOMAIN as entered value in RL settings
            if ( $aImapCredentials['Host'] == "mail.%DOMAIN" || $aImapCredentials['Host'] == "" )
            {
                $aImapCredentials['Host'] = "mail." . $this->getDomainFromEmail($sEmail);
            }
        }
    }

    /**
     * @param string $emailAdress
     * @return string
     */
    private function getDomainFromEmail($emailAddress){

        $parts = explode("@", trim($emailAddress));
        return $parts[1];
    }
}

I intended to add some documentation to the plugin by specifying that using wildcards like %DOMAIN would be replaced by the actual domain used in the email address, but didn't get around to it. Hopefully this can be turned into a more official plugin for allowing such functionality!

Note that this will also work with a wildcard Domain value in the RL settings even if you leave the server fields empty. Perhaps that should be tweaked...

rhyswilliamsza commented 8 years ago

Awesome stuff! Looks perfect already

rhyswilliamsza commented 8 years ago

Alright!

Here we go Jas. You can make a pull request with the extracted folder directly to the RainLoop plugins folder. It is all ready and works well :)

I've put usage instructions and a licence with it, so feel free to change/remove if you'd like.

https://cloud.rhysit.co.za/index.php/s/cXHQZa1PukjtYZn

Rhys

rhyswilliamsza commented 8 years ago

Hi Jas,

Should I make the request?

jas8522 commented 8 years ago

Hey Rhys,

Looks good! Which request is that?

-J

rhyswilliamsza commented 8 years ago

Pull request to the repo :) Let me know if you'd like me to.

Rhys

jas8522 commented 8 years ago

Ah, yes I thought that's what you meant. Go for it!

rhyswilliamsza commented 8 years ago

Cool will do

RainLoop commented 8 years ago

:+1:

RainLoop commented 8 years ago

Small refactoring: https://github.com/RainLoop/rainloop-webmail/commit/e8fbdff09c736e27d8fd1cb6a1eb2b12b55cfd1e

rhyswilliamsza commented 8 years ago

Perfect!

Looks good. I'm sure this plugin will help a lot of those in need :)