Based on the accepted answer, I've created a small solution using the Docker image form the official Dovecot CE repository at https://hub.docker.com/r/dovecot/dovecot, which I'd like to share.
Run the container:
docker run --name dovecot -d dovecot/dovecot
Install sudo, which isn't included in the dovecot image, but needed for our script
apt-get update && apt-get -y install sudo
Set up a mail directory. I found it to be more flexible and less hassle to do so in
/var/mail rather than in the user home directory:
chown vmail /var/mail/vmail
Create a mailbox user, who must be member of the vmail group:
useradd -g vmail theresa
You can add more users anytime if need be.
Tell Dovecot where the mail is. To do so, we need to change
/etc/dovecot/dovecot.conf to this:
You need to restart dovecot afterwards, which can be done by simply restarting the Docker container:
docker restart dovecot
Create the following bash script at
/usr/local/storemail.sh in the dovecot container:
# Store mail in maildir
# Based on https://unix.stackexchange.com/a/82367/215688
# Simply stores an email to the user mail directory
# filename per spec at http://cr.yp.to/proto/maildir.html
rand=$((RANDOM % 1000))
msgname=$(date +%s).P$$R$rand.$(hostname | tr '/:' '\057\072')
# Just in case the user dir has not been created yet
sudo -u vmail mkdir -p "$MAIL_LOCATION/new"
# Safety measure: do not overwrite existing mail
set -o noclobber
# now store the email there
sudo -u vmail cat > "$MAIL_LOCATION/new/$msgname"
Don't forget to make it executable:
chmod +x /usr/local/storemail.sh
Now you can store a new mail using the script. The only argument is the username, and the email needs to be provided from stdin.
storemail.sh theresa <testmail.eml
Or from outside the container use
docker exec -i dovecot <testmail.eml storemail.sh theresa
Or even from a remote machine via ssh
ssh docker-machine.local <testmail.eml docker exec -i dovecot storemail.sh theresa
I recommend to create a custom dockerfile that automates the preparation steps.