Kailash1

Well-Known Member
Nov 27, 2006
259
6
168
cPanel Access Level
Root Administrator
Twitter
Hi,

Is there a way I can setup global setting so that we can not set email forwarding to particular domain.

For example, I do not want to allow email forwarder to @xyz.com from my server. Is it possible to block this for all the domains.

Kailash
 

Be Hard!

Member
Mar 24, 2009
9
1
53
Well, if you want to stop email forwarding an easy wey is to remove the email forwarding feature from your package feature list.

In that case the user wont be able to access to that option in cpanel. Also remember to set :fail: in all domains at /etc/valiases

Cheers!

Be Hard!
Optimization & Security Services for Cpanel
 

cPanelDavidG

Technical Product Specialist
Nov 29, 2006
11,212
13
313
Houston, TX
cPanel Access Level
Root Administrator
I don't want to remove email forwarding feature. I just don't want to allow forwarding to few domains i.e. block email forwarding to AOL, Comcast accounts.

Kailash
If you have a basic understanding of Perl, you can implement this at this time using Custom Event Handlers. This is different from the function hooks we talk about in that function hooks allow you to do additional things after an action has been performed. With Custom Event Handlers, you can create a "pre" handler that intercepts the call to Email::addforward and aborts it if certain conditions are met (e.g. the fwdemail parameter contains comcast.net).

To implement a Custom Event Handler, create a new Perl Module called CustomEventHandler.pm in /usr/local/cpanel/Cpanel/ and create a subroutine called event in the module. You will also need to add this line of code to your event subroutine to pull in all the parameters being passed to it:

Code:
my ( $apiv, [COLOR="Red"]$type[/COLOR], [COLOR="RoyalBlue"]$module[/COLOR], [COLOR="SeaGreen"]$event[/COLOR], [COLOR="DarkOrchid"]$cfgref[/COLOR], $dataref ) = @_;
As for what each of the parameters is:

type is either pre or post. If the type is pre then that means this is before the event is happening. If the type is post, then the event has already happened

module is the name of the module. This will be Cpanel:: followed by the module name from the API1 or API2 function being called. For example: Cpanel::Email

event is the name of the function being called by API1 or API2. For example: addforward

cfgref is a hash of the variables passed to the event. For example: $cfgref{'fwdemail'} would contain the email address to forward email to if email forwarding was the selected option.

dataref is only used in post events. This simply contains the data returned from the event.


Your event subroutine will be called every time an action is performed in the cPanel interface (both before and after). Actions are disallowed by returning false. Therefore, return true by default and only return false when you explicitly do not want something to happen. Otherwise, you may inadvertently disable all cPanel functionality.

For example, you may want to have an if block where you see if the module is Cpanel::Email and the event is addforward and $cfgref{'fwdemail'} contains any of the domains you do not want mail forwarded to, and inside that block of code return false. If that if block is never executed, return true.

Returning false on a pre event will prevent that event from happening. Returning false on a post event only prevents output from that event from being displayed.

We plan to write up formal documentation on this. In the meantime, you can find some documentation in /usr/local/cpanel/hooks/README and the files it references. DO NOT EDIT EventHandler.pm! Instead, create your own CustomEventHandler.pm for your own event handling functions.

I only recommend this if you are familiar with Perl as this can only be done in Perl. If you are not familiar with Perl, I recommend reading a book that can introduce you to the language so you become familiar with the syntax of Perl.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,092
244
368
cPanel Access Level
Root Administrator
If you don't already have a CustomEventHandler.pm in /usr/local/cpanel/Cpanel try adding this attached file into that directory.

Then create a file /etc/forwarder_blocked_domains.txt which contains the domain names of the forwarders you want to block from being created. One on each line.

Example:
Code:
yahoo.com
comcast.net
Now when you try to create a forwarder that uses one of these domain names, you will get an error:

The forwarder could not be added because the form was not complete or would create a mail loop.

Not sure how to change that text, but it will prevent it from being created.

I haven't really tested all of this, so use this at your own risk. If you have problems just delete the CustomEventHandler.pm file.

Be sure that you place this file in the /usr/local/cpanel/Cpanel directory on your server. And be sure that you name the file CustomEventHandler.pm
 

Attachments

Kailash1

Well-Known Member
Nov 27, 2006
259
6
168
cPanel Access Level
Root Administrator
Twitter
If you don't already have a CustomEventHandler.pm in /usr/local/cpanel/Cpanel try adding this attached file into that directory.

Then create a file /etc/forwarder_blocked_domains.txt which contains the domain names of the forwarders you want to block from being created. One on each line.

Example:
Code:
yahoo.com
comcast.net
Now when you try to create a forwarder that uses one of these domain names, you will get an error:

The forwarder could not be added because the form was not complete or would create a mail loop.

Not sure how to change that text, but it will prevent it from being created.

I haven't really tested all of this, so use this at your own risk. If you have problems just delete the CustomEventHandler.pm file.

Be sure that you place this file in the /usr/local/cpanel/Cpanel directory on your server. And be sure that you name the file CustomEventHandler.pm
Hi Sparek,

Sorry for remain silent for few days. Thanks for the script.

Kailash
 

hploh

Member
PartnerNOC
Jun 14, 2006
18
0
151
Hi,

Did any one got idea how to modify the error "
The forwarder could not be added because the form was not complete or would create a mail loop."

to other message such as forwarding to yahoo is not allowed, etc?

Rgds
Loh
 

jenlepp

Well-Known Member
Jul 4, 2005
116
2
168
Liberty Hill, TX
cPanel Access Level
DataCenter Provider
Hi,

Did any one got idea how to modify the error "
The forwarder could not be added because the form was not complete or would create a mail loop."

to other message such as forwarding to yahoo is not allowed, etc?

Rgds
Loh
I modified it through WHM in the templates. Then it got overwritten.

I gave up.
 

youronlineva

Registered
Aug 12, 2015
1
0
1
india
cPanel Access Level
Website Owner
I would prefer not to uproot email sending component. I simply would prefer not to permit sending to couple of spaces i.e. piece email sending to AOL, Comcast accounts.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,884
2,245
463
Before Paper Lantern ate it, that's what it was doing.
Hello :)

Custom event handlers are deprecated as documented at:

Guide to Standardized Hooks - Custom Event Handlers

The following post explains how to block outgoing email to specific domain names:

https://forums.cpanel.net/threads/e...utgoing-mail-to-a-domain.361042/#post-1459812

However, this is intended for all outgoing email and not for forwarders alone. I suggest reviewing the document referenced above for information on how to utilize hookable events.

Thank you.