Moving a Postfix virtual user’s mail to a new address

A user was on too many spam lists and wanted a new mailbox.  He had removed all the spam, and wanted all the existing messages moved over.  The hosting server runs Postfix in a virtual setup.  The script below is what I used.  It makes it easy to move mailboxes in the future for my client.  The script takes two options, and expects the first to be the old email address, the second to be the new email address.  It could stand some error checking, etc., but since I expect it to be run by a clue-possessing administrator, I didn’t put any in here.  This script assumes that the destination mailbox has already been created (in our case, by using PostfixAdmin).

 

#!/bin/bash
mailroot="/var/spool/postfix/virtual"
usersource=$1
userdest=$2
sourcedir="${mailroot}/${usersource}"
destdir="${mailroot}/${userdest}"
echo "Source dir is ${sourcedir}"
echo "Dest dir is ${destdir}"
read -p "Press any key to begin" 
cd ${sourcedir}
for i in *; do
    mv -v $i ${destdir}/
done
cd -

Send a simple message from the command-line

Need to send yourself or someone else a quick email from the Linux command-line?  Use mutt.  First I compose the message in a file, e.g. msg.txt.  Then cat it, and invoke mutt.  The example below also includes an attachment – a  python script I was sending myself.

cat msg.txt | mutt -s "version 2.0" -n -a sample.py me@example.com

Options:
-n – bypass the system configuration
-s – subject
-a – attach a file