python - mail body encoding after procmail processing -
python - mail body encoding after procmail processing -
i've got next line in .procmailrc on smtp server:
body=`formail -i ""` later echo body local file:
echo "$body" >> $home/$filename; \ i've tried prinf (but got same effect):
printf "$body" >> $home/$filename; \ when read file can see encoding has been change. here's got:
administrator system=c3=b3w while should (in polish):
administrator systemów how decode/encode body either straight in .procmailrc or later (bash/python) right string?
another line in .procmailrc works needs additional pipe perl encoder:
subject=`formail -xsubject: | tr -d '\n' | sed -e 's/^ //' | /usr/bin/perl -mencode -ne 'print encode ("utf8",decode ("mime-header",$_ )) '` subject contains utf8 characters , looks ok. maybe there's way utilize similar solution body of mail?
ok. got , running. here's did:
first .procmailrc file:
verbose=yes logfile=$home/procmail.log :0f * ^from.*(some_address@somedomain.com) | $home/python_script.py now python_script.py:
#!/usr/bin/python email.parser import parser import sys temp_file = open("/home/(user)/file.txt","w") temp_file.write("start\n") if not message.is_multipart(): temp_file.write(message.get_payload(decode=true)) else: part in message.get_payload(): if part.get_content_type() == 'text/plain': temp_file.write(part.get_payload(decode=true)) temp_file.close() the hard part debug .procmailrc recipe, had test many options :0, :0f, :0fbw etc... , found 1 suits best.
the next problematic step $body part decoded straight in .procmailrc. figured out solution though, getting rid of stuff , moving python script. tripleee suggested.
it not changed, zapping headers right content-type: header no longer nowadays (you should maintain mime-version: , other standard content-* headers).
you should see, examining source of message in mail service client, procmail or bash have not changed anything. text receive in fact literally administrator system=c3=b3w mime headers inform email client content-transfer-encoding: quoted-printable , content-type: text/plain; charset="utf-8" , knows how decode , display correctly.
if want payload, need decode yourself, in order that, need info mime headers, should not kill them before have handled message (if @ all). this, perhaps:
from email.parser import parser import sys message = parser().parse(sys.stdin) if message['content-type'].lower().startswith('text/'): print(message.get_payload(decode=true)) else: raise diescreaminginanguish('aaaargh!') # pseudo-pseudocode this extremely simplistic in assumes (like current, more broken solution) message contains single, textual part. extending multipart messages not technically hard, how depends on sort of multiparts expect receive, , want payload(s).
like in your previous question suggest move more, or all, of email manipulation python, if going using anyway. procmail has no explicit mime back upwards have reinvent of in procmail, neither simple nor particularly fruitful.
python linux bash procmail
Comments
Post a Comment