Request: Restrict users to specific email domain

Everything related to Hesk - helpdesk software

Moderator: mkoch227

Post Reply
Amphicar770
Posts: 15
Joined: Fri Jun 05, 2009 9:33 pm

Request: Restrict users to specific email domain

Post by Amphicar770 »

/*************************************
Title:
Version:
Author:
Demo:
Download:
Website:

Short description:


*************************************/

Hi all,

just install Hesk and am incredibly impressed.

On thing I would like to be able to do is to limit users to email addresses within my email domain. Thus, if the email address does not end with "@mycompany.com" the user gets a warning message and either has to enter an "@mycompany.com" address or they can go no further.

Thanks,

Mike
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

Try this:

- open file "common.inc.php" (inside "inc" folder) in Notepad or Wordpad
- find this code (lines 202-211):

Code: Select all

    if (preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$address))
    {
        return $address;
    }

    if ($required) {
        hesk_error($error);
    } else {
        return '';
    }
Change it to:

Code: Select all

    if (strpos($address,'@yourcompany.com'))
    {
        return $address;
    }

    if ($required) {
        hesk_error('Sorry, you need a @yourcompany.com address to continue!');
    } else {
        return '';
    }
Didn't test it though, let us know if it works.
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
Amphicar770
Posts: 15
Joined: Fri Jun 05, 2009 9:33 pm

Post by Amphicar770 »

It looks like it is restricting the domain but it is not displaying the error message as defined. Instead, user sees:

"Please enter a valid e-mail address"

Thanks.
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

You can change the text in the language file (inside "language" folder, open it in Notepad).
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
Amphicar770
Posts: 15
Joined: Fri Jun 05, 2009 9:33 pm

Post by Amphicar770 »

Yes, my bad! That fixed it.

I will post as a seperate request, but I am thinking that maybe a better way to handle this is to simply turn off email notifications to the visitor.
JFMichaud
Posts: 9
Joined: Fri Apr 20, 2012 12:06 pm

Re: Request: Restrict users to specific email domain

Post by JFMichaud »

As on 2.3 version is there a way to do the same...

ie: the option have changed as it looks to be on line 990 an on...

i wanna have only 2 possible option @mydomain.com and @sub.mydomain.com

Thanks
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Re: Request: Restrict users to specific email domain

Post by Klemen »

Replacing

Code: Select all

"/([\w\-]+\@[\w\-]+\.[\w\-]+)/"
with this (twice in the code!) should work:

Code: Select all

'/(@domain\.com|@sub\.domain\.com)$/'
Any valid (sub)domains should be within ( ) separated by a |

Instead of . and - use \. and \-

Examples:

Code: Select all

'/(@domain\.com)$/'
'/(@domain\.com|@sub\.domain\.com)$/'
'/(@domain\.com|@sub\.domain\.com|@some\-long\-domain\.com)$/'
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
JFMichaud
Posts: 9
Joined: Fri Apr 20, 2012 12:06 pm

Re: Request: Restrict users to specific email domain

Post by JFMichaud »

Nice...

I've done in common.inc.php

Code: Select all

if ( ! preg_match('/([\w\-]+\@domain\.com|[\w\-]+\@sub\.domain\.com)$/',$v))

Code: Select all

if (preg_match('/([\w\-]+\@domain\.com|[\w\-]+\@sub\.domain\.com)$/',$address))
Thanks
JohnJ
Posts: 4
Joined: Mon Apr 23, 2018 12:54 pm

Re: Request: Restrict users to specific email domain

Post by JohnJ »

Hello,

is this also posible in 2.7.6?

thx.
Klemen
Site Admin
Posts: 10136
Joined: Fri Feb 11, 2005 4:04 pm

Re: Request: Restrict users to specific email domain

Post by Klemen »

The code changed a bit, but still doable; in inc/common.inc.php find

Code: Select all

/* Character not valid in local part unless local part is quoted */
and just ABOVE/BEFORE that code add

Code: Select all

// List of email domains accepted. Everything after @
$accept_domains = array(
    'example.com',
    'sub.example.com',
    'another-example.com',
);

// Only accept emails from domains listed above
if ( ! in_array(strtolower($domain), array_map('strtolower', $accept_domains)) )
{
    return false;
}
I strongly recommend you use a powerful text editor to make changes, such as the free Notepad++ (some editors, including Windows Notepad, won't save PHP files in the correct encoding).
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
JohnJ
Posts: 4
Joined: Mon Apr 23, 2018 12:54 pm

Re: Request: Restrict users to specific email domain

Post by JohnJ »

Thank you so much!
Post Reply