djblets.mail.dmarc¶
Functions for looking up DMARC entries in DNS.
- class DmarcPolicy¶
Bases:
object
Types of DMARC policies.
These policies define what happens if an e-mail fails sender verification (such as if the From address is spoofed).
- NONE = 1¶
No action should be taken if verification fails.
E-mails will not be rejected or quarantined, and the DMARC signature can be ignored.
- QUARANTINE = 2¶
E-mails should be quarantined/marked as spam if verification fails.
- REJECT = 3¶
E-mails should be rejected if verification fails.
- class DmarcRecord(hostname, policy, subdomain_policy=0, pct=100, fields={})¶
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, policy, subdomain_policy=0, pct=100, fields={})¶
Initialize the record.
- Parameters:
hostname (
unicode
) – 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.
- __eq__(other)¶
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:
- classmethod parse(hostname, txt_record)¶
Return a DmarcRecord from a DMARC TXT record.
- Parameters:
- Returns:
The parsed record, if this is a valid DMARC record. If this is not valid,
None
will be returned instead.- Return type:
- __hash__ = None¶
- get_dmarc_record(hostname, use_cache=True, cache_expiration=2592000)¶
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)¶
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.