---
# System prepended metadata

title: 'if `Return-Path` exists, check if domain part contains `From` domain part'

---

require ["fileinto", "reject", "variables", "relational", "envelope", "mailbox"];
​
​
if address :domain :matches "to"   "*" { set "domain_to"   "${1}"; }
if address :domain :matches "from" "*" { set "domain_from" "${1}"; }
if address :matches "from" "*" { set "address_from" "${1}"; }
​
if not header :matches "Message-ID" ["*"] {
    fileinto "INBOX/no-message-id";
}
​
elsif not address :matches "From" "*" {
    fileinto "INBOX/empty-from";
}
​
# if `Return-Path` exists, check if domain part contains `From` domain part
​
elsif allof (address :domain :matches "Return-Path" "*", not address :domain :matches "Return-Path" "*${domain_from}*") {
    discard;
    stop;
}
​
# this should be disabled!
# if `Message-ID` exists, check if domain part contains `From` domain part
​
#elsif allof (address :domain :matches "Message-ID" "*", not address :domain :matches "Message-ID" "*${domain_from}*") {
#    discard;
#    stop;
#}
​
​
if allof ( header :contains "From" "@",
           not header :contains "From" "${domain_from}" ) {
    discard;
    stop;
}
​
# this works, but wont work
# if `From` is defined and `From` contains trusted_phrases
# and domain not in trusted_domains - reject
​
#if allof (header :matches "From" "*",
#          header :contains "From" ["sagiton", "Sagiton", "Czart", "Zimbra", "zimbra"],
#          not string :is "${domain_from}" ["sagiton.pl", "czart.pl"] )  {
#    discard;
#    stop;
#}
​
​
if string :is "${domain_from}" ["sagiton.pl", "czart.pl"] {
    keep;
}
​
​
if header :matches "Reply-To" "*" {
    if not address :domain :is "Reply-To" "${domain_from}" { fileinto "INBOX/different-replyto"; }
    if address :all :contains ["From", "Reply-To"] ["seo", "SEO", "Seo"] { fileinto "INBOX/banned-phrases"; }
}
​
​
if header :matches "Subject" "*" {
    if header :contains "Subject" ["scam", "SCAM", "Scam"] {
        fileinto :create "INBOX/scam";
        stop;
    }
​
    if header :contains "Subject" ["spam", "SPAM", "Spam"] {
        fileinto :create "INBOX/spam";
        stop;
    }
}
​
​
keep;