Could we help you? Please click the banners. We are young and desperately need the money
Spam emails and unwanted newsletters often employ a common technique: using the recipient's username (the part before the @ symbol) in the subject line to create a false sense of personalization. This article shows you how to implement a SpamAssassin rule that detects this pattern and scores emails accordingly.
Spammers frequently include the recipient's username in their subject lines for various reasons:
Copy/Paste the following plugin code and proceed as described in the Installation and Configuration section below:
package Mail::SpamAssassin::Plugin::CheckUsernameInSubject;
use strict;
use warnings;
use Mail::SpamAssassin::Plugin;
use vars qw(@ISA);
@ISA = qw(Mail::SpamAssassin::Plugin);
sub new {
my ($class, $mailsa) = @_;
$class = ref($class) || $class;
my $self = $class->SUPER::new($mailsa);
bless ($self, $class);
$self->register_eval_rule('check_username_in_subject');
return $self;
}
sub check_username_in_subject {
my ($self, $pms, @header_names) = @_;
# Get the subject line
my $subject = $pms->get('Subject');
return 0 unless $subject;
$subject = lc($subject);
# Check each email address in the specified headers
foreach my $header (@header_names) {
my @addresses = $pms->get("${header}:addr");
foreach my $addr (@addresses) {
next unless $addr;
$addr =~ s/^s+|s+$//g;
# Extract username part (everything before @)
if ($addr =~ /^([^@]+)@/) {
my $username = $1;
# Clean up username for comparison
$username = lc($username);
# Check if username appears in subject
if ($subject =~ /bQ$usernameEb/i) {
# Skip if it's a very short username (to avoid false positives)
next if length($username) < 4;
# Skip common words that might be usernames
next if $username =~ /^(info|contact|support|admin|sales|service)$/i;
return 1;
}
}
}
}
return 0;
}
1;
Save the plugin code into a file named username_in_subject.pm
in your SpamAssassin plugin directory (typically):
/usr/share/perl5/Mail/SpamAssassin/Plugin/
or/etc/spamassassin
Add the following lines to your spamassassin config filenano /etc/spamassassin/local.cf
# Load the CheckAddress plugin
loadplugin Mail::SpamAssassin::Plugin::CheckUsernameInSubject check_username_in_subject.pm
### Check if the username part of TO or CC e-mail addresses appears in the subject line.
### For this, the function check_username_in_subject() is used which is a custom function in the plugin username_in_subject.pm
### The plugin extracts the part before the @ symbol from each recipient's email address and checks if it appears in the subject.
### To prevent false positives: Usernames shorter than 4 characters are ignored, and common words like 'info', 'contact', 'support' are skipped.
### This check helps identify spam/newsletters that commonly include the recipient's username in the subject line.
header USERNAME_IN_SUBJECT eval:check_username_in_subject('To','Cc')
describe USERNAME_IN_SUBJECT E-Mail Benutzername im Betreff - Hohe Spam oder Newsletter Wahrscheinlichkeit
score USERNAME_IN_SUBJECT 2.0
After adding these lines, verify the configuration:
spamassassin --lint
spamassassin -D --lint 2>&1 |grep CheckUsername
Nov 12 11:50:45.047 [688419] dbg: plugin: loading Mail::SpamAssassin::Plugin::CheckAddress from /etc/spamassassin/check_username_in_subject.pm
sudo systemctl reload spamassassin
sudo service spamassassin restart
spamassassin -D --test-mode < test_email.txt | grep USERNAME_IN_SUBJECT
The plugin implements several intelligent safeguards to minimize false positives:
b
) to ensure the username appears as a complete wordThe plugin's scoring can be adjusted based on your needs:
The plugin uses several key Perl features:
/^([^@]+)@/
b
) for precise matching/i
flag=~
operator for regex matchingQ...E
quotemeta construct to escape special characters in usernamesThis SpamAssassin plugin provides an effective way to detect spam emails that use recipient usernames in their subject lines. Its intelligent false-positive prevention makes it suitable for production environments, while the configurable scoring allows for fine-tuning based on your specific needs. The implementation is lightweight and efficient, making it an excellent addition to your spam-fighting toolkit.
When implementing this plugin, start with a conservative score and monitor its effectiveness in your environment. The rule can be particularly effective in identifying bulk mailings and certain types of phishing attempts that rely on basic personalization techniques.