/Main_Page

::You must have ninja focus to complete your mission::NinjaFocus::

Email Utility Scripts

Views:

Some random scripts that occasionally help deal with emails (more linux)

Search and Copy Emails

I needed to make an archive of emails from certain users mailboxes. Any emails relating to certain other individuals or containing certain keywords. Everything went in the a shared mailbox so that all the users could see all the communications relating to a project in one place.

The script is a little basic, there were only three mailboxes to trawl, so you have to cd in to each Maildir and set the user name at the top of the script.

#!/bin/bash
NAME="Username";
TARGET_FOLDER="/var/mx-emails/Our_Shared_Maildir";
PATTERNS=`cat <<END_OF_PATTERNS_CONFIGURATION
bob@example.com
jane@example.com
Key Word 1
Foo
regex\s?//\s?Keyword
bar
END_OF_PATTERNS_CONFIGURATION`;


# need to use maildirmake not mkdir
# test -d "${TARGET_FOLDER}" || mkdir "${TARGET_FOLDER}"
# find . -type d -exec test ! -d "${TARGET_FOLDER}/.${NAME}"{} \; -exec mkdir "${TARGET_FOLDER}"/{} \;

find . -maxdepth 1 -name '.?*' -type d | sed -e 's/^\.\///g' | while read FOLDER;
do
	test -d "${TARGET_FOLDER}/.${NAME}${FOLDER}" || maildirmake -f "${NAME}${FOLDER}" "${TARGET_FOLDER}/";
done;

for PATTERN in `echo "${PATTERNS}"`;
do
	echo "Looking for mails containing: ${PATTERN}";
	for EMAIL in `grep -srlHiP "${PATTERN}" * .[^.]?*`;
	do 
		echo "Copying: ${EMAIL}";
		TARGET_EMAIL=`echo "${EMAIL}" | sed -e 's/^cur/\/cur/g'`;
		TARGET_EMAIL=".${NAME}${TARGET_EMAIL}";
		echo "Target: ${TARGET_FOLDER}/${TARGET_EMAIL}";
		cp -p "${EMAIL}" "${TARGET_FOLDER}/${TARGET_EMAIL}";
		touch -r "${EMAIL}" "${TARGET_FOLDER}/${TARGET_EMAIL}";
	done;
done;

Base 64 Decode attachments

I love hot mail, what is it about adding attachments inside another attachment? it's so broken. My mail clients can cope with this.

Save the source of the email, find the attachment you need and cut away all of the other cruft, save the file.

Now use this simple script to decode and spit out the attachment, like this:

$ ./base64decode broken-attachment.txt > some-word-document.doc
#!/opt/local/bin/php
<?php

if (    isset($_SERVER['argv']) && isset($_SERVER['argv'][1]) && 
        is_string($_SERVER['argv'][1]) && is_file($_SERVER['argv'][1])  ){
            
    $code = file_get_contents($_SERVER['argv'][1]);
    echo base64_decode($code);
} else {
    echo "\nFirst argument must be the base 64 encoded text file to decode. Make sure you redirect the output to a new file\n";
    die;
}

// Closing PHP tags is for wimps 

Main Menu

Personal tools

Toolbox