Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
replacement for WebFaction's built-in mail2script
This is a replacement for WebFaction's built-in mail2script (which cannot
parse large messages > 5MB).
To use it:
1. Create an email address that sends messages to a mailbox.
2. Save this script to your WebFaction server and set the mailbox name,
mailbox password, and path to your script.
3. Create a cron job to run this script at a suitable interval, eg:
*/5 * * * * /usr/local/bin/python2.6 /home/username/bin/ > /dev/null 2>&1
Whenever it runs, it will fetch messages that it has not already processed and
pass them to stdin of the script identified by script_path.
import imaplib
import subprocess
import ssl
mailbox = 'your_mailbox_name'
passwd = 'your_mailbox_password'
imaphost = ''
script_path = '/home/username/path/to/your/script'
# log into the mailbox
ctx = ssl.create_default_context()
server = imaplib.IMAP4_SSL(host=imaphost, ssl_context=ctx)
# get all unprocessed messages
resp, items =, "UNSEEN")
items = items[0].split()
# fetch messages and send them to the script
for i in items:
resp, data = server.fetch(i, "(RFC822)")
text = data[0][1].replace('\r','')
p = subprocess.Popen([script_path],stdin=subprocess.PIPE)

This comment has been minimized.

Copy link

commented Mar 28, 2019

Would it be possible to use this somewhere other than Webfaction? I'm moving off of WF with the GoDaddy purchase and going to Linode. I have one site driven by mail2script that I would like to continue using that way on Linode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.