Last active
December 22, 2015 22:49
-
-
Save AndrewRose/6542509 to your computer and use it in GitHub Desktop.
Quick and dirty example of how to interface with SpamAssassin using sockets in PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Copyright 2013 Andrew Rose http://andrewrose.co.uk | |
*/ | |
class spamc | |
{ | |
private static $version = 'SPAMC/1.2'; | |
public static function test($email, $cmd='CHECK', $user='default', $ip='127.0.0.1', $port=783) | |
{ | |
$sock = stream_socket_client('tcp://'.$ip.':'.$port, $errno, $errstr, 30); | |
if (!$sock) | |
{ | |
echo "Failed to connect to spamd, is spamassassin* running?\n"; | |
return FALSE; | |
} | |
$packet = $cmd.' '.self::$version."\n"; | |
$packet .= 'Content-length: '.(strlen($email)+1)."\n"; | |
$packet .= 'User: '.$user."\n\r\n"; | |
fwrite($sock, $packet.$email."\n"); | |
$resp = fread($sock, 256000); | |
switch($cmd) | |
{ | |
/* | |
SPAMD/1.1 0 EX_OK | |
Spam: True ; 7.9 / 5.0 | |
*/ | |
case 'CHECK': | |
{ | |
return trim(explode(':', explode(';', explode("\n", $resp)[1])[0])[1])=='True'?TRUE:FALSE; | |
} | |
break; | |
} | |
} | |
} | |
if(spamc::test('<raw email content here')) | |
{ | |
echo "Spam!\n"; | |
} | |
else | |
{ | |
echo "Not Spam!\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment