djblets.mail.dmarc¶
Functions for looking up DMARC entries in DNS.
- class DmarcPolicy(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
Enum
Types of DMARC policies.
These policies define what happens if an e-mail fails sender verification (such as if the From address is spoofed).
- UNSET = 0[source]¶
A policy is not set on the record.
This can be interpreted as being the same as
NONE
.
- NONE = 1[source]¶
No action should be taken if verification fails.
E-mails will not be rejected or quarantined, and the DMARC signature can be ignored.
- class DmarcRecord(*, hostname: str, policy: DmarcPolicy, subdomain_policy: DmarcPolicy = DmarcPolicy.UNSET, pct: int = 100, fields: Dict[str, str] = {})[source]¶
Bases:
object
Information on a DMARC record for a subdomain or organization domain.
This is a parsed representation of the contents of a standard DMARC TXT record. It contains information that software can use to determine what will happen if a sender spoofs a From address, or if the e-mail otherwise fails sender verification.
Senders can make use of this to determine whether they can safely spoof a From address (for legitimate reasons, such as to send an e-mail on behalf of a user when posting on a service), or whether they should fall back to an alternative means (such as using a noreply address and setting the Reply-To header).
- __init__(*, hostname: str, policy: DmarcPolicy, subdomain_policy: DmarcPolicy = DmarcPolicy.UNSET, pct: int = 100, fields: Dict[str, str] = {}) None [source]¶
Initialize the record.
- Parameters:
hostname (
str
) – The hostname containing the_dmarc.
TXT record.policy (
int
) – The sender policy defined for the record.subdomain_policy (
int
, optional) – The sender policy defined for subdomains on this domain.pct (
int
, optional) – The percentage (as a number from 0-100) of e-mails that should be subject to the sender policy.fields (
dict
, optional) – Additional fields from the record.
- policy: DmarcPolicy¶
The sender policy defined for the record.
- Type:
- subdomain_policy: DmarcPolicy¶
The sender policy defined for subdomains on this domain.
- Type:
- pct: int¶
The percentage of e-mails that should be subject to the sender policy.
This is a range between 0 and 100.
- Type:
- __annotations__ = {'fields': 'Dict[str, str]', 'hostname': 'str', 'pct': 'int', 'policy': 'DmarcPolicy', 'subdomain_policy': 'DmarcPolicy'}¶
- __eq__(other: object) bool [source]¶
Return whether two records are equal.
Records are considered equal if they have the same
hostname
andfields
.- Parameters:
other (
DmarcRecord
) – The record to compare to.- Returns:
True
if the two records are equal.False
if they are not.- Return type:
- __hash__ = None¶
- get_dmarc_record(hostname: str, use_cache: bool = True, cache_expiration: int = 2592000) Optional[DmarcRecord] [source]¶
Return a DMARC record for a given hostname.
This will query the DNS records for a hostname, returning a parsed version of the DMARC record, if found. If a record could not be found for the hostname, the organizational domain will be used instead (which is generally example.com for foo.bar.example.com, but this depends on the domain in question).
By default, the fetched record from DNS is cached, allowing this to be called multiple times without repeated DNS queries. This is optional, as is the expiration time for the cached data (which defaults to 1 month).
- Parameters:
- Returns:
The DMARC record. If it could not be found,
None
will be returned instead.- Return type:
- is_email_allowed_by_dmarc(email_address: str) bool [source]¶
Return whether DMARC rules safely allow sending using an e-mail address.
This will take an e-mail address (which must be in the form of
name@domain
, ideally parsed bymail.utils.parseaddr()
) and check to see if there are any DMARC rules that could prevent the e-mail from being sent/received if it were to fail sender verification.Callers can use this to decide whether they can safely send using a user’s e-mail address, or whether they need to send using the service’s address.