mark107 Posted February 6, 2023 Share Posted February 6, 2023 Hi all, I need your help with the code, because I am not sure which variable I should use to send the report to spamassassin to mark my emails as spam and not spam. I am using the script from this link: https://samprasanna.blogspot.com/2017/06/php-api-to-spamassassin-spamd-protocol.html Here is the code: <?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); require_once('Mail.php'); require_once('Mail/IMAPv2.php'); require('Spamassassin/Client.php'); require('Spamassassin/Client/Result.php'); // Connect to the server: $username = 'username'; $password = 'tester; $params = array( "hostname" => "localhost", "port" => "783", "user" => "user"); $sa = new Spamassassin\Client($params); $inbox = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX'; $inbox = imap_open($inbox, $username, $password) or die("Can't connect: " . imap_last_error()); $email_number = '121'; $message = imap_fetchbody($inbox, $email_number, "", FT_UID); $header = htmlspecialchars(imap_fetchbody($inbox, $email_number, "", FT_UID|FT_PEEK)); ?> It works fine, but I need some help with this: $sa->report($message) Report message as spam, both local and remote And this: $sa->revoke($message) Revokes a message previously reported as spam When I send the report to spamassassin, which variable do I have to use $message or $header to report the email as spam and not spam?? Do I have to use `$message` or `$header` when send report to spamassassin?? I have tried to find the answer on Google, but I couldn't find it. I guess I would have to use the $header variable when I report the emails as spam and not spam but I am not really sure. Any advice would be much appreciated. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/ Share on other sites More sharing options...
ginerjm Posted February 6, 2023 Share Posted February 6, 2023 I do not have any answer for you but I do wonder what you are trying to do. If you have SpamAssassin running, why do you want to run your own php script to identify spam? Isn't that why you have SA running? And how do you intercept the emails before SA gets them? Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605445 Share on other sites More sharing options...
mark107 Posted February 7, 2023 Author Share Posted February 7, 2023 16 hours ago, ginerjm said: I do not have any answer for you but I do wonder what you are trying to do. If you have SpamAssassin running, why do you want to run your own php script to identify spam? Isn't that why you have SA running? And how do you intercept the emails before SA gets them? I just want to add the records in the SpamAssasin when I mark the emails as spam, so the emails will be identify as spam next time I receive them. I don't know why you ask me these questions. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605450 Share on other sites More sharing options...
ginerjm Posted February 7, 2023 Share Posted February 7, 2023 I asked because I figure that SA is doing this work already so wondered why you felt the need to do it yourself. Have at it. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605451 Share on other sites More sharing options...
mark107 Posted February 7, 2023 Author Share Posted February 7, 2023 (edited) Oh right, so what is the main reason that most providers use mark as spam?? When I mark the emails as spam, does it add to the spamassassin rules so spamassassin will learn from it?? Edited February 7, 2023 by mark107 Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605452 Share on other sites More sharing options...
gizmola Posted February 7, 2023 Share Posted February 7, 2023 There is nothing in the spamassassin daemon api that is designed for spam marking. As far as I know what you need to do with spamassassin is run sa-learn to train it: https://geekthis.net/post/spamassassin-training/ Notice that it is just as important to have the "ham" as it is to have the "spam". So in general, you want to get your box in a state, where you have marked the spam, and then run sa-train on those boxes. In general, marking something as spam in a client is basically for the purposes of moving it to a spam folder that has a retention limit on it that will automatically delete the spam emails when the email gets to a particular age. Of course in doing so, based on your configuration you can then use that as the basis for running sa-learn, perhaps as a cron job. FWIW large emails sites can't trust the userbase to train their systems, and there is also mail filtering that goes on to knock down a lot of spam before it ever gets to the point of being in the mail system. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605456 Share on other sites More sharing options...
mark107 Posted February 7, 2023 Author Share Posted February 7, 2023 How I can run sa-learn to train it using with PHP?? Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605463 Share on other sites More sharing options...
kicken Posted February 7, 2023 Share Posted February 7, 2023 (edited) The way I setup my email server is to just have sa-learn run nightly and train the junk folder as spam and the inbox (excluding new unread messages) as ham. Marking something as spam just requires moving it to the junk folder. Setup something similar on your server then just move the message to the appropriate folder using PHP. Edited February 7, 2023 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605465 Share on other sites More sharing options...
mark107 Posted February 7, 2023 Author Share Posted February 7, 2023 29 minutes ago, kicken said: The way I setup my email server is to just have sa-learn run nightly and train the junk folder as spam and the inbox (excluding new unread messages) as ham. Marking something as spam just requires moving it to the junk folder. Setup something similar on your server then just move the message to the appropriate folder using PHP. How do you use your PHP to run sa-learn to train the emails as junk?? I need to see it how you did it so I can run for a test, if you dont show it I won't know how you did it. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605468 Share on other sites More sharing options...
kicken Posted February 7, 2023 Share Posted February 7, 2023 1 hour ago, mark107 said: How do you use your PHP to run sa-learn to train the emails as junk I don't. I just setup a cron job to run it each night. I don't have PHP involved in the email setup at all. Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605477 Share on other sites More sharing options...
mark107 Posted February 7, 2023 Author Share Posted February 7, 2023 1 hour ago, kicken said: I don't. I just setup a cron job to run it each night. I don't have PHP involved in the email setup at all. Oh right, what command do you use to put in the cron job to get it to run in every 24 hours?? How do you call the spamassassin to add the rules when you mark the emails as spam? Do you use the same API as I use? Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605482 Share on other sites More sharing options...
kicken Posted February 7, 2023 Share Posted February 7, 2023 1 minute ago, mark107 said: Oh right, what command do you use to put in the cron job to get it to run in every 24 hours?? I have a simple cron entry like this to run a script each day: @daily /root/bin/update-junk.sh The update-junk.sh is a small shell script that runs the different sa-learn commands. cd /var/lib/spamassassin/vconfig sa-learn --spam --dbpath ./example.com/mailbox/bayes /var/spool/mail/virtual/example.com/mailbox/.Junk/cur sa-learn --spam --dbpath ./example.com/mailbox/bayes /var/spool/mail/virtual/example.com/mailbox/.Junk/new sa-learn --ham --dbpath ./example.com/mailbox/bayes /var/spool/mail/virtual/example.com/mailbox/cur 5 minutes ago, mark107 said: How do you call the spamassassin to add the rules when you mark the emails as spam? I don't. I use Thunderbird for my mail client and when I mark something as junk it moves the message into the Junk folder. Spamassassin will process it as spam next time the @daily cron job runs. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315887-mark-emails-as-spam-and-not-spam-in-php/#findComment-1605483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.