Jump to content

Mark emails as spam and not spam in PHP


mark107

Recommended Posts

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.

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by kicken
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.