Extensive sendmail wrapper with sender throttling

In this tutorial I’d like to describe how to create an extensive sendmail wrapper for a web server to monitor all sent emails and throttle daily sent email volume by the senders original UID (user id). This is useful if you e.g. run PHP in CGI-mode with SuExec, that is: all customers run their scripts under their own UID. The wrapper described here is not just a PHP-only wrapper (as described in my Simple PHP mail wrapper tutorial) – it directly replaces /usr/sbin/sendmail so we are able track all sent email of the whole system.


On a Debian/Ubuntu system, make sure you have the the following packages installed:

Now create the database sendmailwrapper and the one and only needed table throlltle in MySQL:

Let’s now move the original /usr/sbin/sendmail and replace it with our wrapper script (download below and put it into e.g. /opt/sendmail-wrapper/). Make sure, we got a logfile where all web-users are allowed to write to and only root can read from:

Install the following cronjobs for daily cleanup and reporting (download cleanup.pl and report.pl below and put them into /opt/sendmail-wrapper/):

The script now throttles sent email to a limit of 1000/day and prints log output to /var/www/log/sendmail-wrapper.log, e.g.:

This has been tested on a Debian Etch system running Perl 5.8.8 / MySQL 5.0.32 / exim4 (4.63). It works as well with Postfix but there are known problems with mailq and similar commands that pass parameters to /usr/sbin/sendmail.

Download needed wrapper files and don’t forget to configure them properly (CONFIGURATION section):

6 Responses

  1. Boris
    Jul 08, 2009 - 01:26 PM


    i have a Problem with it, if i install all described here, i start a mail.php

    and the wrapper starts to loop and try to send the message initiated from the php script till its maximum throttle count.

    So it looks like.
    If i have a look to top, i see that the scripts starts 100 sendmail processes, but dosnt send anything.

    If i disable logging, it starts only 1 process, but no message was sent.

    Can you help ?

    Best regards


  2. Boris
    Jul 08, 2009 - 01:34 PM

    Problem fixed 🙂
    have inside the script the path to sendmail directed to sendmail. Simply a Bad idea 😉 Pointed to sendmail.orig and now it runs. Thanks


  3. Andreas
    Feb 04, 2011 - 08:52 PM

    You should update your installer script. If you are running with Postfix this may destroy the newaliases tool. Just relink it to the orig sendmail:
    rm /usr/bin/newaliases; ln -s /usr/sbin/sendmail.orig /usr/bin/newaliases
    This could avoid unusual side effects.

  4. claudio
    Nov 29, 2013 - 09:40 AM

    After the installation on my Server Debian 6.0.7 with Plesk 11.5.30 an Roundcube 0.8.6, Roundcube is not working anymore…
    It pops up with an error: An error occured, sending of message failed..
    Do you have a idea why..  or did anybody had the same problem..
    thanks for help


  1. Extensive sendmail wrapper with sender throttling | PHP-Blog.com
  2. PHPscript verstuurd spam - webhostingtalk.nl

Leave a Comment