Skip to content

Instantly share code, notes, and snippets.

@tasoseng
Last active March 26, 2021 22:57
Show Gist options
  • Save tasoseng/ba127824cb3e91726ec9f1bfa239577f to your computer and use it in GitHub Desktop.
Save tasoseng/ba127824cb3e91726ec9f1bfa239577f to your computer and use it in GitHub Desktop.
distribute sympa moderated emails
#!/usr/bin/python2
# coding: utf8
import sys
import re
import imaplib
import smtplib
from getpass import getpass
user = raw_input('Username: ')
password = getpass('Password: ')
fromaddr = "from@example.com"
toaddr = "sympa@lists.example.com"
IMAP_SERVER = '172.16.0.1'
IMAP_PORT = '993'
SMTP_SERVER = '172.17.0.25'
SMTP_PORT = '587'
FOLDER='lists'
M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
M.login(user, password)
M.select(FOLDER)
typ, data = M.search(None, 'UNSEEN')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
#text = data[0][1].decode('utf-8')
text = data[0][1]
# check if sender is me
result = re.search('^Subject: .* from ' + fromaddr , str(text), re.M)
if( not result ):
print('Rejected, message not from ' + fromaddr)
continue
result = re.search('^DISTRIBUTE.*$', text, re.M)
if ( result ):
print(result.group(0))
msg = "From: " + fromaddr + "\n"
msg += "To: " + toaddr + "\n"
msg += "Subject: " + result.group(0) + "\n"
msg += "\n\n"
conn = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
conn.starttls()
conn.login(user, password)
#conn.set_debuglevel(1)
conn.sendmail(fromaddr, toaddr, msg)
conn.quit()
else:
print("Not found")
M.close()
M.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment