Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
replacement for WebFaction's built-in mail2script
#!/usr/local/bin/python2.6
"""
mailbox2script.py
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/mailbox2script.py > /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 = 'mail.webfaction.com'
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)
server.login(mailbox,passwd)
# get all unprocessed messages
server.select()
resp, items = server.search(None, "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)
p.stdin.write(text)
p.stdin.close()
@Nolapete

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.

@defulmere

This comment has been minimized.

Copy link
Owner Author

commented Oct 6, 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.

Sure, there's nothing here that depends on WebFaction. Adjust your hostnames, paths, and credentials as needed and it should work.

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.