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");
 }