Snippets

How can I DKIM sign an e-mail?

To DKIM sign an email you can use the following code in your filter:

use Mail::MIMEDefang::DKIM;  
my ($header, $value) = md_dkim_sign('domain-dkim.key.pem', 'rsa-sha256', 'relaxed/simple', $sender_domain, 'dkim');  
action_insert_header($header, $value);
How can I verify a DKIM signature ?

To verify a DKIM signature you can use the following code in your filter:

use Mail::MIMEDefang::DKIM;  
my ($result, $domain, $keysize, $btag) = md_dkim_verify();  
if($result eq "pass") {  
  ...  
}
How can I convert winmail.dat TNEF attachments to their original content?

To convert all attached winmail.dat TNEF attachments to their original content you can use the following code in your filter:

sub filter {
  my($entity, $fname, $ext, $type) = @_;

  if (lc($type) eq "application/ms-tnef" or lc($fname) eq "winmail.dat" ) {
    use Convert::TNEF;
    use File::Type;
    use File::Temp qw(tempfile tempdir);

    my $tnefdir = tempdir(CLEANUP => 1, DIR => "/tmp");
    my $tnef = Convert::TNEF->read_ent($entity,{output_dir=>"$tnefdir"});

    my $ft = File::Type->new();

    for ($tnef->attachments) {
         my $mimetype = $ft->mime_type($_->data);

         my $tnef_entity = action_add_part($entity, "$mimetype", "base64", $_->data, $_->longname, "attachment");
         md_graphdefang_log('tnef_ext', "File: " . $_->longname . " Type: $mimetype");

         filter ($tnef_entity, $_->longname, "", "$mimetype");
    }
    $tnef->purge;
    return action_drop();
  }
  return action_accept();
}
How can I use rspamd to detect spam messages ?

To use rspamd to detect spam messages you can use the following code in your filter:

 my ($hits, $req, $names, $report, $action, $is_spam) = rspamd_check();
 md_syslog("Warning", "Action: $action, Spam: $is_spam, Names: $names");
 if ($is_spam eq "true") {
   action_change_header("X-Spam-Score", "$hits/$req $names");
   md_syslog("Warning", "Action: $action");
   md_graphdefang_log('spam', $hits, $RelayAddr);
 } else {
   # Delete any existing X-Spam-Score header?
   action_delete_header("X-Spam-Score");
 }
How can I implement greylisting ?

In order to use use greylisting you should add this code tou your filter:

my $dbh;
sub filter_initialize {
    my($entity) = @_;

    $dbh = DBI->connect($dsn, $username, $auth, {RaiseError => 1});
}

sub filter_recipient {
    my ($recipient, $sender, $ip, $hostname, $first, $helo,
        $rcpt_mailer, $rcpt_host, $rcpt_addr) = @_;

    my $ip_address = $ip;

    # Greylist all the /24 subnet
    #
    # my @ip = split(/\./, $ip);
    # $ip_address = $ip[0] . '.' . $ip[1] . '.' . $ip[2] . '.0';

    my $ret = Mail::MIMEDefang::Actions::action_greylist($dbh, $sender, $recipient, $ip_address);
    if($ret eq "tempfail") {
      return('TEMPFAIL', "Email greylisted, please come back later");
    } elsif($ret eq "reject") {
      return('REJECT', "Go away or deliver email faster");
    } else {
      return ('CONTINUE', "ok");
    }
}

 sub filter_cleanup {
   $dbh->disconnect();
 }

A sample database schema is available in the contrib/greylisting directory.