• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Extension Dev - Handling Mails

lippoliv

Basic Pleskian
Hey there,
I want to develope an extension for my Plesk servers to do some check of outgoing mails before they will be delivered.

I created an Plesk Extension and setup an test-server. So now I read the documentation about Mail Handlers (http://docs.plesk.com/en-US/12.5/extensions-guide/mail-handlers.71844/)
The hooking point "Before local delivery" or "Before remote delivery" looks quiet the right place to work with.

But how do I catch them up in my hook/-Folder? Seems like Plesk just have 5 hooks to extend:
pm_Hook_Auth, pm_Hook_ContentInclude, pm_Hook_CustomButtons, pm_Hook_Interface and pm_Hook_LoginContentInclude?

I saw the sectioni "Registering Mail Handlers" (http://docs.plesk.com/en-US/12.5/ex...ing-handlers/registering-mail-handlers.71855/), so may I could set up an Controller and register him as an handler, but that sounds not good. Also, the mail_handler_controller expects an executable file...?

May some other could help me out with some information.
 
The hooking point in terms of mail handlers doesn't related with pm_Hook* methods in SDK, it's very different things. The mail_handlers_control utility accept executable (binary or script) as one of arguments, and the email messages will be passed to this binary, so you need to create separate script, which would be this handler.
 
Hey mizar,
thanks for reply.

That script could be PHP? Or do it have to be Shell?

Is there an example out there for this use-case?
 
And next question: How can I register my executable while installing the Plesk extension, and de-register while removing the plesk extension?
 
Hello,
And next question: How can I register my executable while installing the Plesk extension, and de-register while removing the plesk extension?
It's simple - in extension post-install you need to call mail_handlers_control --add, and in pre-uninstall of extension you can mail_handlers_control --remove.

Hey mizar,
thanks for reply.

That script could be PHP? Or do it have to be Shell?

Is there an example out there for this use-case?

It should be just executable, can be shell or php or python or whatever. I'll try to provide an example here
 
Hello,

Mail handler accepts the email message from stdin, process it and returns to stout. The simple handler, which add the new header looks this (error checking is omitted):
Code:
#!/bin/sh

sender="$1"
sender="$2"
context="$3"

msg=`cat -`
echo "X-Processed-By: my-test-handler"
echo $msg
echo "PASS" >&2
exit 0

And it can be registered (if placed into /opt/psa/handlers/hooks/my-test-handler):
plesk sbin mail_handlers_control --add --priority 50 --queue=before-local --type global --executable /opt/psa/handlers/hooks/my-test-handler --enabled --name test-handler
 
Hey mizar,
Ok for debugging purposes I deployed an SHELL script with my Plesk Extension and run the following command:
Code:
plesk sbin mail_handlers_control --add --priority 50 --queue=before-queue --type global --executable /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh --enabled --name mail-handler
Now I get
Code:
root@v22016081573736335:~# plesk sbin mail_handlers_control --list
.---.---.-------.--------------------------------------.-----------------.------------------.-----------------.
| E | P | prior |  address  |  name  |  type  |  queue  |
|---|---|-------|--------------------------------------|-----------------|------------------|-----------------|
|  |  |  10 |  all |  spf |  global |  before-queue |
| X | X |  50 |  all |  mail-handler |  global |  before-queue |
| X |  |  10 |  all-recipients |  check-quota |  global |  before-queue |
| X |  |  10 |  all-recipients |  check-quota |  global | before-sendmail |
'---'---'-------'--------------------------------------'-----------------'------------------'-----------------'
My script looks like yours with an small extend
Code:
#!/bin/sh

sender="$1"
sender="$2"
context="$3"

msg=`cat -`
echo "X-Processed-By: my-test-handler"
echo $msg
echo "PASS" >&2

logger "X-Processed-By: my-test-handler"
logger $msg
logger "PASS" >&2

exit 0

So now I should get some entrys to /var/log/messages AND in the mail header, if i understand your script correct. But both doesn't appear.
The script exists and is "+x"
Code:
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP
total 8
-rw-r--r-- 1 root root 136 Aug  2 09:51 index.php
-rwxr-xr-x 1 root root 220 Aug 19 09:36 mail-handler.sh
 
Hey mizar,
Ok for debugging purposes I deployed an SHELL script with my Plesk Extension and run the following command:
Code:
plesk sbin mail_handlers_control --add --priority 50 --queue=before-queue --type global --executable /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh --enabled --name mail-handler
Now I get
Code:
root@v22016081573736335:~# plesk sbin mail_handlers_control --list
.---.---.-------.--------------------------------------.-----------------.------------------.-----------------.
| E | P | prior |  address  |  name  |  type  |  queue  |
|---|---|-------|--------------------------------------|-----------------|------------------|-----------------|
|  |  |  10 |  all |  spf |  global |  before-queue |
| X | X |  50 |  all |  mail-handler |  global |  before-queue |
| X |  |  10 |  all-recipients |  check-quota |  global |  before-queue |
| X |  |  10 |  all-recipients |  check-quota |  global | before-sendmail |
'---'---'-------'--------------------------------------'-----------------'------------------'-----------------'
My script looks like yours with an small extend
Code:
#!/bin/sh

sender="$1"
sender="$2"
context="$3"

msg=`cat -`
echo "X-Processed-By: my-test-handler"
echo $msg
echo "PASS" >&2

logger "X-Processed-By: my-test-handler"
logger $msg
logger "PASS" >&2

exit 0

So now I should get some entrys to /var/log/messages AND in the mail header, if i understand your script correct. But both doesn't appear.
The script exists and is "+x"
Code:
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP
total 8
-rw-r--r-- 1 root root 136 Aug  2 09:51 index.php
-rwxr-xr-x 1 root root 220 Aug 19 09:36 mail-handler.sh
Hello,

You could check /var/log/maillog to see if handler executed, it should looks like:
Code:
Aug 24 11:03:23 mybox postfix/smtpd[27184]: connect from unknown[10.10.10.10]
Aug 24 11:03:23 mybox postfix/smtpd[27184]: EE742C0A91: client=unknown[10.10.10.10]
Aug 24 11:03:23 mybox postfix/cleanup[27189]: EE742C0A91: message-id=<57bd2a9a94fee_359d11370e44449@mail>
Aug 24 11:03:23 mybox /usr/lib/plesk-9.0/psa-pc-remote[1009]: handlers_stderr: SKIP
Aug 24 11:03:23 mybox /usr/lib/plesk-9.0/psa-pc-remote[1009]: SKIP during call 'check-quota' handler
Aug 24 11:03:23 mybox /usr/lib/plesk-9.0/psa-pc-remote[1009]: handlers_stderr: PASS
Aug 24 11:03:24 mybox /usr/lib/plesk-9.0/psa-pc-remote[1009]: PASS during call 'mail-handler' handler
Aug 24 11:03:24 mybox postfix/smtpd[27184]: disconnect from unknown[10.10.10.10]

BTW, how will you send the message for handler check ?
 
Hey there,
OK my test-handler seems to have an problem...?

Code:
Aug 19 09:26:58 v22016081573736335 postfix-local[15632]: postfix-local: [email protected], [email protected], dirname=/var/qm$
Aug 19 09:26:58 v22016081573736335 postfix-local[15632]: Error during 'mail-handler' handler
Aug 19 09:26:58 v22016081573736335 postfix-local[15632]: deliveryManager: Get empty message id, delivery state will be disabled for this mail
Aug 19 09:26:58 v22016081573736335 dovecot: service=lda, [email protected], ip=[]. msgid=unspecified: saved mail to INBOX
Aug 19 09:26:58 v22016081573736335 postfix/pipe[15631]: 9D78A1006D1: to=<[email protected]>, relay=plesk_virtual, delay=57, delays=56/0.05/0/0.35, dsn$
Aug 19 09:26:58 v22016081573736335 postfix/qmgr[15308]: 9D78A1006D1: removed

Any idea to get more details abot "Error during 'mail-handler' handler"?
 
Hello,

Error during 'mail-handler' handler means that execution of handler is failed - for instance handler binary is not executable, or system user popuser does't have enough permissions to run it
 
OK thanks for your information.

Indeed the file was root.root so I change it:

Code:
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
-rwxr-xr-x 1 root popuser 220 Aug 19 09:36 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh

now I monitored the following

Code:
root@v22016081573736335:~# tail -f /var/log/maillog
Aug 25 12:03:14 v22016081573736335 postfix/smtpd[24218]: connect from ip4d15ba79
Aug 25 12:04:38 v22016081573736335 postfix/smtpd[24218]: 9DD3C1002BB: client=ip4
Aug 25 12:04:46 v22016081573736335 postfix/cleanup[24224]: 9DD3C1002BB: message-id=<>
Aug 25 12:04:46 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: SKIP
Aug 25 12:04:46 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: SKIP during call 'check-quota' handler
Aug 25 12:04:46 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: Mail handler call failed. Error occured during execv(/opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh):#012No such file or directory
Aug 25 12:04:46 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: .
Aug 25 12:04:46 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: Error during 'mail-handler' handler
Aug 25 12:04:46 v22016081573736335 postfix/qmgr[15308]: 9DD3C1002BB: from=<[email protected]>, size=270, nrcpt=1 (queue active)
Aug 25 12:04:46 v22016081573736335 postfix-local[24230]: postfix-local: [email protected], [email protected], dirname=/var/qmail/mailnames
Aug 25 12:04:46 v22016081573736335 postfix-local[24230]: deliveryManager: Get empty message id, delivery state will be disabled for this mail
Aug 25 12:04:47 v22016081573736335 dovecot: service=lda, [email protected], ip=[]. msgid=unspecified: saved mail to INBOX
Aug 25 12:04:47 v22016081573736335 postfix/pipe[24229]: 9DD3C1002BB: to=<[email protected]>, relay=plesk_virtual, delay=8.6, delays=7.7/0.04/0/0.8, dsn=2.0.0, status=sent (delivered via plesk_virtual service)
Aug 25 12:04:47 v22016081573736335 postfix/qmgr[15308]: 9DD3C1002BB: removed

heres the ls -l

Code:
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
-rwxr-xr-x 1 root popuser 220 Aug 19 09:36 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh

Shouldn't it work?
 
Hello,

The handler is executed, but it return wrong value. Coold you attach or send me in private handler file ?

Thanks,
 
You mentioned the line

Code:
Sep  1 10:26:50 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: Mail handler call failed. Error occured during execv(/opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh):#012No such file or directory

right? Looks like plesk doesnt find that handler...
 
Hello,

Still cannot reproduce your issue:
  1. I copy&paste your handler into /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
  2. make it executable: chmod 0755 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
  3. Register it via plesk sbin mail_handlers_control --add --priority 50 --queue=before-queue --type global --executable /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh --enabled --name mail-handler
  4. Then sent message from external server to email on this box, and handler executed properly:
Sep 1 21:36:04 a10-52-145-36 /usr/lib/plesk-9.0/psa-pc-remote[31729]: handlers_stderr: PASS
Sep 1 21:36:04 a10-52-145-36 /usr/lib/plesk-9.0/psa-pc-remote[31729]: PASS during call 'mail-handler' handler

Could you check permissions on /opt/psa/admin/htdocs/modules/PMSP and probably EOL-style in script (should be unix-style, not win) ?
 
Hey there,
within PhpStorm I changed from CRLF to LF for EOL (so it's unix style now). Also I did the chown to make sure all is like your configuration.

Code:
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
-rwxr-xr-x 1 root popuser 205 Sep  2 11:00 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
root@v22016081573736335:~# chmod 775 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
root@v22016081573736335:~# ls -l /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh
-rwxrwxr-x 1 root popuser 205 Sep  2 11:00 /opt/psa/admin/htdocs/modules/PMSP/mail-handler.sh

AND NOW IT WORKED :)

Code:
Sep  2 11:03:17 v22016081573736335 postfix/smtpd[21555]: improper command pipelining after MAIL from ip4d15bed9.dynamic.kabel-deutschland.de[77.21.190.217]: RCPT TO: [email protected]\r\nDATA\r\n
Sep  2 11:03:17 v22016081573736335 postfix/smtpd[21555]: D09E51006EA: client=ip4d15bed9.dynamic.kabel-deutschland.de[77.21.190.217]
Sep  2 11:03:20 v22016081573736335 postfix/cleanup[21559]: D09E51006EA: message-id=<>
Sep  2 11:03:20 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: SKIP
Sep  2 11:03:20 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: SKIP during call 'check-quota' handler
Sep  2 11:03:20 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: handlers_stderr: PASS
Sep  2 11:03:20 v22016081573736335 /usr/lib/plesk-9.0/psa-pc-remote[15289]: PASS during call 'mail-handler' handler
Sep  2 11:03:20 v22016081573736335 postfix/cleanup[21559]: D09E51006EA: milter-reject: END-OF-MESSAGE from ip4d15bed9.dynamic.kabel-deutschland.de[77.21.190.217]: 4.7.1 Service unavailable - try again later; from=<[email protected]> to=<[email protected]> proto=SMTP

Thank you for your effort to get this done with me :)

Now I can start the real development of my Plugin.
 
Last puzzle seems to be to chown / chmod from post-install.php.

Code:
Sep  2 13:04:14 v22016081573736335 plesk sendmail[30921]: handlers_stderr: SKIP
Sep  2 13:04:14 v22016081573736335 plesk sendmail[30921]: SKIP during call 'check-quota' handler
Sep  2 13:04:14 v22016081573736335 plesk sendmail[30921]: handlers_stderr: Mail handler call failed. Error occured during execv(/opt/psa/admin/plib/modules/PMSP/scripts/mail-handler.sh):#012Permission denied
Sep  2 13:04:14 v22016081573736335 plesk sendmail[30921]: handlers_stderr: .
Sep  2 13:04:14 v22016081573736335 plesk sendmail[30921]: Error during 'PMSP' handler
Sep  2 13:04:15 v22016081573736335 postfix/pickup[30256]: E9CDE100745: uid=10001 from=<[email protected]>
Sep  2 13:04:15 v22016081573736335 postfix/cleanup[30928]: E9CDE100745: message-id=<[email protected]>
Sep  2 13:04:15 v22016081573736335 postfix/qmgr[26601]: E9CDE100745: from=<[email protected]>, size=402, nrcpt=1 (queue active)
Sep  2 13:04:15 v22016081573736335 postfix/smtp[30930]: E9CDE100745: to=<[email protected]>, relay=none, delay=0.1, delays=0.09/0.01/0/0, dsn=5.4.4, status=bounced (Host or domain name not found. Name service error for name=invaliddomain123123123123.de type=AAAA: Host not found)
Sep  2 13:04:15 v22016081573736335 postfix/cleanup[30928]: 0F577100749: message-id=<[email protected]>
Sep  2 13:04:15 v22016081573736335 postfix/qmgr[26601]: 0F577100749: from=<>, size=2525, nrcpt=1 (queue active)
Sep  2 13:04:15 v22016081573736335 postfix/bounce[30931]: E9CDE100745: sender non-delivery notification: 0F577100749
Sep  2 13:04:15 v22016081573736335 postfix/qmgr[26601]: E9CDE100745: removed
Sep  2 13:04:15 v22016081573736335 postfix-local[30934]: postfix-local: from=MAILER-DAEMON, [email protected], dirname=/var/qmail/mailnames
Sep  2 13:04:15 v22016081573736335 postfix-local[30934]: Unable to get sender domain by sender mailname
Sep  2 13:04:15 v22016081573736335 postfix-local[30934]: cannot chdir to mailname dir test: No such file or directory
Sep  2 13:04:15 v22016081573736335 postfix-local[30934]: Unknown user: [email protected]
Sep  2 13:04:15 v22016081573736335 postfix/pipe[30933]: 0F577100749: to=<[email protected]>, relay=plesk_virtual, delay=0.04, delays=0.01/0.01/0/0.02, dsn=2.0.0, status=sent (delivered via plesk_virtual service)
Sep  2 13:04:15 v22016081573736335 postfix/qmgr[26601]: 0F577100749: removed
 
Hello,

Yes, /opt/psa/admin/plib/modules/PMSP/scripts/mail-handler.sh should be executable. And probably mail-handler should be placed into sbin/, not plib
 
Back
Top