Skip to content
 

PHP API to SpamAssassin spamd Protocol

On the past few days I’ve been working on a PHP spamd client, and I think it is somewhat usable for now since it already implements all of the protocol commands. This is release 0.1.0, so you might expect some API changes for the next versions. Feedback is always welcome. I tried to make this the most straight-forward as possible, abstracting some nasty things that happens under the hood, so you can do things like:

require_once 'SpamAssassin/Client.php';
 
$params = array(
    "hostname" => "localhost",
    "port" => "783",
    "user" => "ppadron",
);
 
$sa = new SpamAssassin_Client($params);
 
$message = file_get_contents('/path/to/message/file');
 
var_dump($sa->isSpam($message));
bool(true)

Methods available

ping()
Pings the server to test the connection
getSpamReport($message)
Returns a detailed report if the message is spam or null if it’s ham
headers($message)
Processes the message and returns it’s headers (like X-Spam-Flag and X-Spam-Status)
check($message)
Checks if a message is spam with the CHECK protocol command
isSpam($message)
Shortcut to check() method that returns a boolean
process($message)
Processes the message, checks it for spam and returning it’s modified version
symbols($message)
Returns all rules matched by the message
learn($message, $learnType)
Uses SpamAssassin learning feature with TELL. Must be enabled on the server.
report($message)
Report message as spam, both local and remote
revoke($message)
Revokes a message previously reported as spam

I’m still working on providing some good usage examples, but I’ve put (temporarily) API docs available here. In the meantime, you can browse the tests directory and learn some basic stuff. If you want to use this library to implement a spam filter for your mail server, maybe you should consider using the spamc command line utility or the Perl librarythat already comes with SpamAssassin (which was my main source of inspiration). This library is most useful when you already have stuff written in PHP and you need to do something with SpamAssassin. If you have a webmail written in PHP and would like to interact with a remote SpamAssassin server to report spam, for example. The project is hosted @ Github, so you can:

I owe you a PEAR repo to serve this package.

9 Comments

  1. Really good idea, i will check your progress on this project ;)

  2. nrafal says:

    Hi!

    When you write protocol version as number, you may suffer from some local specyfics. E.g. in Poland we are writing 1,5. PHP knows that, so it’s giving me “SPAMC/1,5\r\n”, which causes problems. Changed it to string, ’1.5′, and everything is right now ;)

    sorry for bad english :) greetings :)

  3. HKS says:

    Do you know if anyone has tried using this for blog comment spam?

  4. Pedro Padron says:

    @nrafal: This was fixed in 0.3.0, thanks!

  5. Pedro Padron says:

    @HKS: Hmm… don’t know about that, but I wouldn’t recommend it. Spamassassin’s rules are focused on email spam, analyzing headers + body. You’d need to tweak your spamassassin to only use only rules that will be applied to the body of the message. If you are using WordPress, I’d recommend using a plugin like Akismet.

  6. Ken Guest says:

    Nice work! It’d be super to see this go into the main PEAR channel itself, tbh.

  7. gabriel says:

    Hello
    looks nice but i have this error:
    Fatal error: Uncaught exception ‘SpamAssassin_Client_Exception’ with message ‘Could not connect to SpamAssassin: Connection refused’ in…

    i believe this is because connexion from outside localhost are not permitted (spamd is up and listening on 127.0.0.1 783

    how can i fix this ? Muit Obrigao

  8. Pedro Padron says:

    Hi Gabriel,
    If your spamassassin server needs to be accessible from outside, you need to start the spamd with the -i , otherwise it will listen only on 127.0.0.1. In Debian/Ubuntu, you can add this flag to the OPTIONS variable in the /etc/default/spamassassin file.

  9. gabriel says:

    hello and thank you for your reply
    it’s getting better, but i have one problem:

    problem is permission denied: server disallow php to access the mail files. what permissions should be set, and how ?

    thank you !

Leave a Reply