postfix - access to incoming mail info inside of python script
Clash Royale CLAN TAG#URR8PPP
postfix - access to incoming mail info inside of python script
I use postfix
for mail server and use procmail
for doing some stuff like extracting attachments and create directory for every message and send all data to another server.
postfix
procmail
This is my postfix config :
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Debian specific: Specifying a file name will cause the first
# line of that file to be used as the name. The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname
smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
biff = no
# appending .domain is the MUA's job.
append_dot_mydomain = no
# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h
readme_directory = no
# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:$data_directory/smtpd_scache
smtp_tls_session_cache_database = btree:$data_directory/smtp_scache
# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = imiMailServerHost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, XXX.YYYY.com, splnx, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.100.0/24
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
home_mailbox = Maildir/
And this is my .procmailrc config that I call a bash script to do stuff :
VERBOSE=on
LOGFILE=/home/ali/procmail.log
:0
| /home/ali/Maildir/a.sh
And this is a.sh :
#!/usr/bin/bash
/usr/bin/ripmime -i - -d Maildir/ --prefix;
/usr/bin/python3 /home/ali/Maildir/a.py;
The extraction is done successfully, but i can't access to my mail info like subject or body or ... inside my python script.
How can i access to arrived mail info inside python script ?
Is there any better solution to create folder for every receipt message in postfix ?
Subject:
1 Answer
1
Python itself contains an email
library which certainly allows you to extract text from the headers and body, with somewhat higher precision than the pure regex you are confined to with Procmail. (Especially MIME structures are tricky to handle in Procmail.)
email
If you don't want that, and there is a small static set of headers you want for every message, regex may well be sufficient. Procmail allows you to pull out part of a regex match using the /
special token. The captured text is available in the special variable MATCH
.
/
MATCH
:0 # The whitespace between [...] is a space and a tab
* ^Subject:[ ]*/.+
SUBJECT=$MATCH
:0
| /home/ali/Maildir/a.sh -s "$SUBJECT"
The Procmail variables you define yourself will be exported, so you can in fact access $SUBJECT
from within a.sh
directly; this creates a different complication because you have to explicitly set this variable while testing the script, but it might be simpler for your scenario than implementing option parsing.
$SUBJECT
a.sh
A third option is to hard-code script arguments so that $1
is the subject, $2
is the sender, etc; but this is horrible from a usability perspective, so not something I want to recommend. (If you do it anyway, you need to document it in the script source at the very least, for the sake of your own sanity.)
$1
$2
I would in fact refactor this to just call ripmime
and python
directly from Procmail.
ripmime
python
:0
* ^Subject:[ ]*/.+
SUBJECT=$MATCH
:0c
| ripmime -i - -d Maildir/ --prefix
:0
| python3 Maildir/a.py -s "$SUBJECT"
(As an aside, don't hardcode the paths to ripmime
and python3
. Instead, make sure your PATH
is sane. Probably also make sure a.py
has a proper shebang line and mark it executable, and take out the python3
.)
ripmime
python3
PATH
a.py
python3
Your current solution needlessly complicates the question by calling Python from a shell script which you call from Procmail. If Python isn't central to the actual question, maybe something like
:0
| b.sh
where b.sh
replaces a.sh
and a.py
with something like
b.sh
a.sh
a.py
#!/bin/sh
t=$(mktemp -t -d procmbXXXXXXXXX) || exit
# clean up when done or if interrupted
trap 'rm -rf "$t"' EXIT HUP INT QUIT TERM
cat >"$t"/00input.msg
ripmime -i "$t"/00input.msg -d "$t" --prefix
# as per your updated requirement in a comment
scp "$t" remotehost:messages/"$(basename "$t")"
If your real question is actually "where did Procmail save this message?", the Procmail variable $LASTFOLDER
contains the file name.
$LASTFOLDER
Piping the message to Python is slightly different from your current solution, but probably for the better.
– tripleee
May 19 at 6:21
Thx for your answer, the second code not work and this
procmail[2268]: Suspicious rcfile "/home/ali/.procmailrc"
shown .but the python part alone is work properly . i want to extract attachments in separate folder that name base is on name of new mail and then run python code to completely transfer it(mail and all attachments) to another server .– alireza
May 20 at 5:36
procmail[2268]: Suspicious rcfile "/home/ali/.procmailrc"
"Suspicious rcfile" indicates a problem with the file's ownership or permissions. Make sure your
.procmailrc
is owned by and only writable by yourself.– tripleee
May 20 at 5:53
.procmailrc
Your updated problem statement sounds like you don't need Python here at all. Write a shell script which runs
ripmime
on the saved message and then runs scp
to copy the results somewhere else. But it also sounds a bit unclear anl broad - maybe post a separate question about that?– tripleee
May 20 at 6:00
ripmime
scp
See updated answer now
– tripleee
May 20 at 6:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Every message has a
Subject:
header; if you want the action to trigger unconditionally, just don't put a condition.– tripleee
May 19 at 5:43