pymailq.store – Mails queue storage objects¶
The store
module provide several objects to convert mails queue content
into python structures.
PostqueueStore
Objects¶
- class
pymailq.store.
PostqueueStore
[source]¶Postfix mails queue informations storage.
The
PostqueueStore
provides methods to load Postfix queued mails informations into Python structures. Thoses structures are based onMailHeaders
classes which can be processed by aMailSelector
instance.The
PostqueueStore
class defines the following attributes:
loaded_at
¶
datetime.datetime
instance to store load date and time informations, useful for datas deprecation tracking. Updated onload()
call withdatetime.datetime.now()
method.
postqueue_cmd
¶
list
object to store Postfix command and arguments to view the mails queue content. This property use Postfix mails content parsing command defined inpymailq.CONFIG
attribute under the key ‘list_queue’. Command and arguments list is build on call with the configuration data.
spool_path
¶Postfix spool path string. Default is
"/var/spool/postfix"
.
postqueue_mailstatus
¶Postfix known queued mail status list. Default is
['active', 'deferred', 'hold']
.
mail_id_re
¶Python compiled regular expression object (
re.RegexObject
) provided byre.compile()
method to match postfix IDs. Recognized IDs are either:
- hexadecimals, 8 to 12 chars length (regular queue IDs)
- encoded in a 52-character alphabet, 11 to 16 chars length (long queue IDs)
They can be followed with
*
or!
. Default used regular expression is:r"^([A-F0-9]{8,12}|[B-Zb-z0-9]{11,16})[*!]?$"
.
mail_addr_re
¶Python compiled regular expression object (
re.RegexObject
) provided byre.compile()
method to match email addresses. Default used regular expression is:r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+$"
MailClass
¶The class used to manipulate/parse mails individually. Default is
The
PostqueueStore
instance provides the following methods:
PostqueueStore.
load
([method])[source]¶Load content from postfix mails queue.
Mails are loaded using postqueue command line tool or reading directly from spool. The optionnal argument, if present, is a method string and specifies the method used to gather mails informations. By default, method is set to
"postqueue"
and the standard Postfix queue control tool: postqueue is used.Optionnal argument
parse
controls whether mails are parsed or not. This is useful to load every known mail headers for later filtering.
Parameters: Provided method
str()
name is directly used withgetattr()
to find a self._load_from_<method> method.
PostqueueStore.
_load_from_postqueue
()[source]¶Load content from postfix queue using postqueue command output.
Output lines from
_get_postqueue_output
are parsed to buildC0004979687 4769 Tue Apr 29 06:35:05 sender@domain.com (error message from mx.remote1.org with parenthesis) first.rcpt@remote1.org (error message from mx.remote2.org with parenthesis) second.rcpt@remote2.org third.rcpt@remote2.orgParsing rules are pretty simple:
Line starts with a valid
Mail.qid
: create newqid
,size
,date
andsender
informations from line.
Queue ID Size Reception date and time Sender C0004979687 4769 Tue Apr 29 06:35:05 user@domain.com Line starts with a parenthesis: store error messages to last created
errors
attribute.Any other matches: add new recipient to the
recipients
attribute of the last createdOptionnal argument
filename
can be set with a file containing output of the postqueue command. In this case, output lines of postqueue command are directly read fromfilename
and parsed, the postqueue command is never used.Optionnal argument
parse
controls whether mails are parsed or not. This is useful to load every known mail headers for later filtering.
Parameters:
PostqueueStore.
_load_from_spool
()[source]¶Load content from postfix queue using files from spool.
Mails are loaded using the command defined in
postqueue_cmd
attribute. Some informations may be missing using the_load_from_spool()
method, including at leastMail.status
field.Optionnal argument
parse
controls whether mails are parsed or not. This is useful to load every known mail headers for later filtering.Loaded mails are stored as
mails
attribute.
Parameters: parse (bool) – Controls whether loaded mails are parsed or not. Warning
Be aware that parsing mails on disk is slow and can lead to high load usage on system with large mails queue.
PostqueueStore.
_get_postqueue_output
()[source]¶Get Postfix postqueue command output.
This method used the postfix command defined in
postqueue_cmd
attribute to view the mails queue content.Command defined in
postqueue_cmd
attribute is runned using asubprocess.Popen
instance.
Returns: Command’s output lines. Return type: list()
See also
- Python module:
subprocess
– Subprocess management
PostqueueStore.
_is_mail_id
(mail_id)[source]¶Check mail_id for a valid postfix queued mail ID.
Validation is made using a
re.RegexObject
stored in themail_id_re
attribute of thePostqueueStore
instance.
Parameters: mail_id (str) – Mail Postfix queue ID string Returns: True or false Return type: bool()
PostqueueStore.
summary
()[source]¶Summarize the mails queue content.
Returns: Mail queue summary as dict
Sizes are in bytes.
Example response:
{ 'total_mails': 500, 'total_mails_size': 709750, 'average_mail_size': 1419.5, 'max_mail_size': 2414, 'min_mail_size': 423, 'top_errors': [ ('mail transport unavailable', 484), ('Test error message', 16) ], 'top_recipient_domains': [ ('test-domain.tld', 500) ], 'top_recipients': [ ('user-3@test-domain.tld', 200), ('user-2@test-domain.tld', 200), ('user-1@test-domain.tld', 100) ], 'top_sender_domains': [ ('test-domain.tld', 500) ], 'top_senders': [ ('sender-1@test-domain.tld', 100), ('sender-2@test-domain.tld', 100), ('sender-7@test-domain.tld', 50), ('sender-4@test-domain.tld', 50), ('sender-5@test-domain.tld', 50) ], 'top_status': [ ('deferred', 500), ('active', 0), ('hold', 0) ], 'unique_recipient_domains': 1, 'unique_recipients': 3, 'unique_sender_domains': 1, 'unique_senders': 8 }
Mail
Objects¶
- class
pymailq.store.
Simple object to manipulate email messages.
This class provides the necessary methods to load and inspect mails content. This object functionnalities are mainly based on
email.message.Message
instance’s stored informations are extracted to extendInitialization of
Parameters:
- mail_id (str) – Mail’s queue ID string
- size (int) – Mail size in Bytes (Default:
0
)- date (datetime.datetime) – Acceptance date and time in mails queue. (Default:
None
)- sender (str) – Mail sender string as seen in mails queue. (Default: empty
str()
)The
qid
¶Mail Postfix queue ID string, validated by
_is_mail_id()
method.
size
¶Mail size in bytes. Expected type is
int()
.
parsed
¶
bool()
value to track if mail’s content has been loaded from corresponding spool file.
parse_error
¶Last encountered parse error message
str()
.
status
¶Mail’s queue status
str()
.
sender
¶Mail’s sender
str()
as seen in mails queue.
recipients
¶Recipients
list()
as seen in mails queue.
errors
¶Mail deliver errors
list()
as seen in mails queue.
head
¶Mail’s headers
MailHeaders
structure.
postcat_cmd
¶This property use Postfix mails content parsing command defined in
pymailq.CONFIG
attribute under the key ‘cat_message’. Command and arguments list is build on call with the configuration data.The
Mail.
parse
()[source]¶Parse message content.
This method use Postfix mails content parsing command defined in
postcat_cmd
attribute. This command is runned usingsubprocess.Popen
instance.Parsed headers become attributes and are retrieved with help of
message_from_string()
function provided by theSee also
- Postfix manual:
- postcat – Show Postfix queue file contents
Mail.
dump
()[source]¶Dump mail’s gathered informations to a
dict
object.Mails informations are splitted in two parts in dictionnary.
postqueue
key regroups every informations directly gathered from Postfix queue, whileheaders
regroupsMailHeaders
attributes converted from mail content with theparse()
method.If mail has not been parsed with the
parse()
method, informations under theheaders
key will be empty.
Returns: Mail gathered informations Return type: dict
MailHeaders
Objects¶
- class
store.
MailHeaders
[source]¶Simple object to store mail headers.
Object’s attributes are dynamically created when parent
parse()
is called. Those attributes are retrieved with help ofmessage_from_string()
method provided by theStandard RFC 822-style mail headers becomes attributes including but not limited to:
- Received
- From
- To
- Cc
- Bcc
- Sender
- Reply-To
- Subject
Case is kept while creating attribute and access will be made with
Mail.From
orMail.Received
for example. All those attributes will return list of values.See also
- Python modules:
email.message.Message
– Representing an email messageRFC 822 – Standard for ARPA Internet Text Messages