Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Sunday, January 20, 2013

Extract Email Attachments With Python

If you archive your email messages, like me, you may find that you want to pull out all of the attachments for those files so your desktop search will parse them better, or so you can quickly search through them.

This is a simple script that just recurses through your .eml messages in a directory and pulls out all of the base64 encoded attachments.

For those of you that are wondering what base64 is, it's an encoding that only uses sixty-four different characters to transmit information. The email system uses this to send documents around so that the protocol didn't have to be reconfigured to account for stuff that wasn't text.

Code

#!/usr/bin/env python

import email.parser
import os
import sys
import base64


fileList = []
rootdir = "/path/to/.eml/messages/"
for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(os.path.join(root,file))

id = 0

for path in fileList:
if not path.endswith(".eml"):
continue

fp = email.parser.FeedParser()
fp.feed(open(path).read())

message = fp.close()

for message in message.walk():
fn = message.get_filename()
if fn == None:
continue

try:
with open(fn, 'wb') as out:
out.write(base64.b64decode(message.get_payload()))
except TypeError:
with open(fn, 'wb') as out:
out.write(message.get_payload())

Extensions

  • This script isn't very efficient being that it uses python to decode.
  • It would be nice to pull arguments from the command line using sys.argv

Update 2013-09-04 Python 3


#!/usr/bin/env python3

import email.parser
import os
import sys
import base64
import binascii
import sys


def extract(rootdir):
fileList = []

for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(os.path.join(root,file))

for path in fileList:
if not path.endswith(".eml"):
continue

fp = email.parser.BytesFeedParser()
fp.feed(open(path, "rb").read())

message = fp.close()

print("Checking {}".format(path))

for message in message.walk():
fn = message.get_filename()
if fn == None:
continue
try:
try:
with open(fn, 'wb') as out:
out.write(message.get_payload(decode=True))
except (TypeError, binascii.Error):
with open(fn, 'wb') as out:
print(message.get_payload())
out.write(bytes(message.get_payload(), message.get_charset()))
except Exception:
print("Error extracting item from {}".format(path))

if __name__ == "__main__":
if len(sys.argv) == 1:
print("usage: {} path/to/.eml/files".format(sys.argv[0]))
exit(1)
extract(sys.argv[1])

Saturday, December 29, 2012

Get CRON to Send Email Reports

Here is a quick tip: if you have CRON running jobs, but want assurance they're getting done by way of email, do the following:
Open CRON:
crontab -e
You'll see something like this:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

28 * * * * cd /home/joseph/google_drive && grive
Now add the following line to the end of the file:
MAILTO=somebody@your.domain
Then Save and Close; as long as you have a mail server set up and running, you'll get an email with the output of each of the commands in the file every time they're run.

If You Don't Have a Mail Server

If you don't have a mail server set up, and are running Ubuntu, you may want to check out Juju Charms they are Ubuntu's newest way to set up complex server configurations with a single command. Or just follow this tutorial on how to set up Postfix.