We deal with email a lot and one thing with handling email is to verify where it came from. We must make sure when we receive email from someone@gmail.com, it really was sent from gmail.com server, not from someone else's email server. SPF or Sender Policy Framework is what being used for that. For all our apps, we used libspf which we wrapped into a convenience python extension that can be used across projects. As time goes by, we want to start depreciating all our C code and move everything (hopefully) to Python.

There's a Python module called pyspf. It looks like what we're looking for although the documentation seems to suggest that it part of bigger module called pymilter. I can't find any documentation showing how to use only the SPF checking utility (if it has one) and after digging through the source code, I found out the function that we need. It exists as spf.check2() function.

In order to use pyspf we also need it dependency which called pydns. The following command should cover us well:-

easy_install pyspf
easy_install pydns

The module then available for import under the name spf. You can import it like below:-

import spf

The function signature for spf.check2 function look like this:-

def check2(i, s, h, local=None, receiver=None):

where i for ip address, s for the sender email address, and h for the email address domain part. So we can do our check for example as:-

>>> spf.check2('180.73.166.174', 'someone@gmail.com', 'gmail.com')
('neutral', 'access neither permitted nor denied')

The return value would be in form result, explanation which is a tuple. result can either be 'pass', 'fail', 'permerror', 'temperror', 'softfail', 'none' or 'neutral'. We run side by side test with our existing spf function and the result seem to match so we're quite confident this function is what we're looking for. Now it less one C code that we have to maintain.

Kamal Mustafa

Kamal Mustafa

CTO

Joined December 2010. Have been doing web development since early 2000 with classic ASP, then moved on to PHP and finally involved with Python/Django since joining Xoxzo Inc. During his spare time Kamal hacks various open source projects at github.