stalwartlabs / mail-auth

DKIM, ARC, SPF and DMARC library for Rust
https://docs.rs/mail-auth/
Apache License 2.0
82 stars 13 forks source link

Allow external usage of parsing functions for SPF, DMARC and DKIM structs #30

Closed Miesvanderlippe closed 3 months ago

Miesvanderlippe commented 3 months ago

This is useful for other projects that want to use the parsing functionality on its own, without parsing an entire e-mail.

Closes #29

Some test code:

use mail_auth::{common::parse::TxtRecordParser, spf::Spf, dmarc::Dmarc, dkim::Signature};

fn main() {
    let spf_record = "v=spf1 A:example.com MX -all";
    match Spf::parse(spf_record.as_bytes()) {
        Ok(parsed_record) => {
            println!("SPF: {:?}", parsed_record);
        }, 
        Err(_) => { println!("Whoops"); }
    }

    let dmarc_record = "v=DMARC1; p=quarantine; adkim=s; aspf=s; rua=mailto:report@example.com; pct=100; fo=0:1:d:s;";

    match Dmarc::parse(dmarc_record.as_bytes()) {
        Ok(parsed_record) => {
            println!("Dmarc: {:?}", parsed_record);
        }, 
        Err(_) => { println!("Whoops"); },
    }

    let dkim_signature = "DKIM-Signature: v=1; a=rsa-sha256; s=jun2005.eng; c=relaxed/relaxed; d=example.com; s=dkim; t=1526555738; bh=mhU6OJb5ldZf+z/pX9+0Nc4tj/lmyYHWbR8LgI2Q=; h=To:From:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding; b=s1sdZCzdX9vxocsMSlT7cOrYixl1g8wfkdzrVe7BGN6ZdPV9xu2A";
    match Signature::parse(dkim_signature.as_bytes()) {
        Ok(parsed_record) => {
            println!("Dkim: {:?}", parsed_record);
        }, 
        Err(_) => { println!("Whoops"); },
    }
}
Miesvanderlippe commented 3 months ago

Thanks for the quick merge @mdecimus :)