[−][src]Trait mda::EmailRegex
Trait providing convenience methods for regular expression searching
in emails. The trait methods can be use with the byte data returned by
the Email::header
, Email::body
and Email::data
methods.
This trait treats and searches the email contents as bytes. The regular
expression parsing is configured for case-insensitive and multi-line
search (i.e., ^
and $
match beginning and end of lines respectively).
In addition to the single regular expression searching, a method for matching regular expression sets is provided. This can be more efficient than matching multiple regular expressions independently.
All the trait methods will fail if the regular expression is invalid, or the searched email data isn't valid utf-8.
Required methods
fn search(&self, regex: &str) -> Result<bool>
Returns whether the contents match a regular expression.
Example
use mda::{Email, EmailRegex}; let email = Email::from_stdin()?; if email.header().search(r"^To:.*me@example.com")? { email.deliver_to_maildir("/my/maildir/path")?; }
fn search_with_captures(&self, regex: &str) -> Result<Option<Captures>>
Returns the capture groups matched from a regular expression.
Example
use std::path::Path; use mda::{Email, EmailRegex}; let email = Email::from_stdin()?; if let Some(captures) = email.header().search_with_captures(r"^X-Product: name=(\w+)")? { let name = std::str::from_utf8(captures.get(1).unwrap().as_bytes()).unwrap(); email.deliver_to_maildir(Path::new("/my/maildir/").join(name))?; }
fn search_set(&self, regex_set: &[&str]) -> Result<SetMatches>
Returns the matches from a set of regular expression. This can be more efficient than matching multiple regular expressions independently.
Example
use mda::{Email, EmailRegex}; let email = Email::from_stdin()?; let matched_sets = email.header().search_set( &[ r"^To: confidential <confidential@example.com>", r"^X-Confidential: true", ] )?; if matched_sets.matched_any() { email.deliver_to_maildir("/my/mail/confidential/")?; }