How to test sending email using MIME::Lite

Earlier this week I had to update a system that uses the MIME::Lite module to send email so that the emails it sends are properly UTF-8 encoded. The change to the code was very simple.

What was a little less simple was writing a test to verify that email is properly encoded. Note that I stated that I was writing a test. When developing software it is not enough to manually test that something does what is expected. We write tests so that we can repeatedly prove that our systems work as they are expected to and also so that we can identify if a later change breaks something.

Thankfully I was programming in Perl which means I had a flexible language to work with. The solution was to redefine MIME::Lite’s send method like so:


no warnings "redefine";
*MIME::Lite::send = *MIME::Lite::as_string;
use warnings "redefine";

What this does is firstly to turn off warnings for redefines. If you use warnings in your Perl programming you will get a runtime warning when you redefine a subroutine or method and we don’t want that warning as we fully intend to do this. Do note that the warnings on redefine are re-enabled immediately once we’re done though.

After we’ve ensured that Perl wont raise a warning we go ahead and redefine the send() method. In this case we redefine it to MIME::Lite’s own as_string method which simply returns the entire email, headers and body, as a single string. The code which calls send() captures the return value and sends it up the calling stack so we will get this back. Even though the code expects to be sending back true or false the fact that Perl doesn’t do strict variable typing means we can do this safely. You can redefine to anything. It’s fairly common to redefine to something as simple as sub { return $_[0] } but in this case MIME::Lite has as_string so we used that.

With this in place I was free to simply call higher level methods in the classes and objects which were in turn using MIME::Lite objects to send email and safely test the resulting emails.

Posted in Code, Email, Perl, Testing

Why you should learn perl programming

Last weekend I attended the London Perl Workshop. One of the discussions was centred on the state of the jobs market for Perl programmers.

If you listen to many in the media and those who measure the popularity of programming languages Perl might not seem like the best choice for someone who is considering a career in programming. You might well end up considering that Java, C, Python or even PHP are where you should be focusing and certainly the first three are good bets however what you would be missing is that there is a very strong demand in the real jobs market for Perl developers.

Not only is there strong demand but pay rates are also very good as that demand is generally from successful companies. Moreover those who program in Perl benefit from the availability of a vast range of well written libraries on the CPAN which make it easier to produce high quality code that is easy to maintain. The days when you might reasonably fear that Perl programming resulted in spaghetti code not even the author could maintain are long gone. Modern perl programming produces clear code with well established standards which mean large teams are able to work well together.

I recently found myself having to dig into a very large Perl codebase (over 100,000 lines of code) which is already nearly 10 years old. One might expect that this was a painful experience but in fact that was far from the truth. The code was clear, readable and easy to update without fear of breaking things.

The fact that Perl has a fantastic testing framework (courtesy of CPAN) makes life even better as it is easy to write and maintain a suite of tests to ensure that changes to the software do exactly what is expected and do not have unexpected results.

Of course Perl is a Free Software/Open Source language so you get all the benefits of continuous development with an immediate focus on what developers and those who employ them actually need and of a dynamic and, mostly, friendly and intelligent community around the language.

UKFSN uses the LAMP stack and here the P is definitely Perl.

Posted in Uncategorized

Preventing SMTP Auth abuse

One of the biggest problems I have had to deal with over the past few years has been abuse of authenticated SMTP. Spammers occasionally manage to obtain a customer’s username and password and use these to send email via our servers with authenticated SMTP.

It’s not as easy to prevent this as it is to prevent other attempts to block spammers as we need to ensure that genuine customers are able to send email without problems. The solution is not however enormously difficult and a discussion I had today has encouraged me to publish the generic bits of it so others can use it or pick holes as appropriate.

WordPress does horrible things to the code indenting so you’ll need to forgive the horrible style.

#!/usr/bin/perl
use Tie::Hash::Expire;
use File::Tail;

if ( $> != 0 ) {
    die "You must be root to run this command";
}
my %seen = ();
tie %seen, 'Tie::Hash::Expire', { expire_seconds => 1200 };
my $line = undef;
my $log = File::Tail->new(name => "/var/log/mail.log", tail => -1);
while ($line = $log->read) {
    next unless $line =~ /: client=.*\[(.*)\], sasl_method=(.*), sasl_username=(.*)/;
    my $ip = $1;
    my $user = $3;
    $user =~ s/, sasl_sender=.*$//;

    $seen->{$user}->{$ip}++;

    my $size = keys %{$seen->{$user}};
    if ($size > 2 ) {
        # "Customer $user looks like they have been compromised\n";
        my ($l, $d) = split /@/, $user;
        if ( $d ) {
            # full email address - local part is $l and domain is $d
            # XXX Do something here to suspend the account - change password?
        }
    }
    else {
        # just a username. Again suspend it
    }
 }

This should be fairly clear. All it does is to tail the mail log file and track the sasl login and the IP address of the client connection. If the same user login connects to the authenticated SMTP service from more than 2 client IP addresses within a short time some action is taken to suspend the account.

On our real implementation of this there is a further check on the client IP address so that we don’t track connections if we recognise the IP address as belonging to a customer broadband connection.

Posted in Code, Email, Perl, Postfix, SPAM

Fibre just got even faster

Our Fibre Broadband service is now faster than ever. The standard Fibre service which based upon FTTC, where the connection from your home or business goes to the street cabinet and is connected to the fibre network there, now offers speeds of up to 80Mb/s download and up to 20Mb/s upload.

Our first customer to upgrade is enjoying a download connection rate of 76.06Mb/s.

For some customers – although currently only a small number – we can offer even faster broadband with Fibre directly into your home or business offering download speeds of up to 110Mb/s.

Why not check on our website to see if you can get Fibre Broadband today?

Posted in Broadband, Enta, Fibre Broadband

Broadband and IPv6

We’ve been offering IPv6 services with our Allowance based broadband services for some time however I wasn’t able to give a recommendation for an inexpensive router capable of routing IPv6 on a broadband connection – mostly because I was not using it on my own broadband connection. UKFSN does use IPv6 on our servers but that’s another matter.

This week the router I have been using on the end of my own broadband connection (Fibre Broadband from UKFSN!) died but that was not a disaster as I had been planning to replace it and so already had it’s replacement on hand. The replacement is a Routerbox 750 from Mikrotik which I bought from Linitx.com

Setting it up was very easy. All I had to do was to configure the PPPoE client in the PPP section. Initially IPv6 didn’t seem to work so I checked and there is a firmware update available to version 5.12. That was easily applied – just FTP the files to the router and then reboot it.

Once I did that I added the IPv6 addresses to the router’s configuration which had to be done manually as the DHCPv6 client did not register the assignment however it’s trivially easy to do – I just set the link /64 on the PPPoE interface and added the /56 allocation to the internal network. Then when I restarted networking on my PC it picked up a /64 and routing just worked.

For just £32 these Mikrotik Routerbox 750 routers are fantastic although they do have far more features than I will ever use!

Posted in Broadband, Enta, IPv6

Email Delays

Our incoming email systems have been stalled for much of today due to an undetected failure on the email filtering server. I am working on remedying it as I type this and I expect it to be fixed within the next hour.

Posted in Uncategorized

Free Fibre Broadband Installation/Migration

We are able to offer Fibre Broadband installation free of charge to anyone who is connected via a capable line on the following BT exchanges:

BARNET
CATFORD
HULME HALL
MITCHAM
OLDHAM
PORTSMOUTH
STAMFORD HILL

If you know anyone living in these areas and you like them point them to us. We’ll arrange the installation, including the BT engineer visit and supply of the Fibre broadband modem at no charge.

Posted in Uncategorized