Changeset 2:108a82defd3e in mailjam for postman
- Timestamp:
- May 15, 2012, 2:29:03 AM (13 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- postman
- Files:
-
- 6 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
postman/config.py
r1 r2 4 4 5 5 # The default path for storage files 6 storage_path = os.path.join(os.path.dirname(__file__), 'db') 6 storage_path = os.path.join(os.path.dirname(__file__), 'storage') 7 8 # The default path for the archives 9 archive_path = os.path.join(os.path.dirname(__file__), 'archives') 7 10 8 11 # Set to True to set that, by default, only emails from members -
postman/models.py
r1 r2 3 3 import os 4 4 from tools import validate_email_address 5 from storage import JsonStorage 5 from storage import JsonStorage as Storage 6 6 import config 7 7 … … 38 38 self.members = members 39 39 self.config = config 40 self.storage = JsonStorage(os.path.join(config['storage'], 41 self.address)) 40 self._validate_config() # validate the config parameters 41 self.storage = Storage(os.path.join(self.config['storage'], 42 self.address)) 43 self.archive = Storage(os.path.join(self.config['archive'], 44 self.address)) 45 # try to load data from the storage 46 loaded = self.load() 47 # FIXME: if loaded is False, the storage does not exist, perhaps 48 # this would be a good place to create it for the first time 42 49 43 50 def __repr__(self): … … 50 57 if not 'storage' in self.config.keys(): 51 58 self.config['storage'] = os.path.join(config.storage_path, 52 self.address)59 'mailings/') 53 60 if not 'archive' in self.config.keys(): 54 self.config['archive'] = os.path.join(self.config['storage'], 55 'archive') 61 self.config['archive'] = config.archive_path 56 62 if not 'private' in self.config.keys(): 57 self.config['private'] = config.private 63 self.config['private'] = config.private_mailing 58 64 59 def _validate_member_object(self, member ):65 def _validate_member_object(self, member=None): 60 66 if not isinstance(member, Member): 61 67 raise TypeError(member, ' is not a valid Member instance') 62 68 return member 63 69 64 def _validate_member(self, member ):70 def _validate_member(self, member=None): 65 71 member = self._validate_member_object(member) 66 72 return member.address in self.members_addresses() 67 73 68 def _validate_member_by_address(self, address ):74 def _validate_member_by_address(self, address=None): 69 75 if not validate_email_address(address): 70 76 raise ValueError(address, ' is not a valid email address') … … 81 87 return True 82 88 83 def add_member_by_address(self, address ):89 def add_member_by_address(self, address=None): 84 90 if self._validate_member_by_address(address): 85 91 return False … … 88 94 return True 89 95 90 def delete_member(self, member ):96 def delete_member(self, member=None): 91 97 member = self._validate_member_object(member) 92 98 if not self._validate_member(member): … … 101 107 self.address = data.address 102 108 self.members = data.members 109 return True 110 return False 103 111 104 112 def save(self): -
postman/mta.py
r1 r2 8 8 class Sendmail(): 9 9 10 def __init__(self, mailing_list ):10 def __init__(self, mailing_list=None): 11 11 if not isinstance(mailing_list, MailingList): 12 12 raise ValueError(mailing_list, ' is not a valid mailing list') 13 13 self.mailing_list = mailing_list 14 14 self.suscriptors = self.mailing_list.members_addresses 15 self.reply_address = self.mailing_list.address 15 self.reply_to = self.mailing_list.address 16 self.raw_email = None 16 17 self.queue = [] 17 18 self.archive = self.mailing_list.config.get('archive', 18 config. storage_path)19 config.archive_path) 19 20 20 def get_raw_email( ):21 def get_raw_email(self): 21 22 try: 22 raw_email = sys.stdin.read()23 self.raw_email = sys.stdin.read() 23 24 except: 24 25 raise IOError('Can not get a valid email from stdin') 25 return raw_email26 return self.raw_email 26 27 27 def save_raw_email(): 28 def save_raw_email(self): 29 if not self.raw_email: 30 # FIXME: perhaps a while loop here, with some maximum recursion 31 # check, would be nice here 32 self.get_raw_email 28 33 filename = os.path.join(self.archive, 29 34 datetime.today().strftime('%Y%d%m%H%M%S%f')) 30 35 tmpfile = file(filename, 'w') 31 tmpfile.write( raw_email)36 tmpfile.write(self.raw_email) 32 37 tmpfile.close() 33 38 self.queue.append(filename) 34 39 35 def send_email(): 36 if not self.queue: 37 raise ValueError('The emails queue is empty') 38 next_email = self.queue.pop() 39 email_file = file(next_email, 'r') 40 email_data = email.message_from_file(email_file) 41 email_file.close() 40 def send_email(self): 41 if self.queue: 42 next_email = self.queue.pop() 43 email_file = file(next_email, 'r') 44 email_data = email.message_from_file(email_file) 45 email_file.close() 42 46 43 email_data['Reply-to'] = self.reply_address47 email_data['Reply-to'] = self.reply_to 44 48 45 smtp_conn = smtplib.SMTP() 46 smtp_conn.connect() 47 smtp_conn.sendmail(email_data['From'], self.suscriptors, 48 email_data.as_string()) 49 smtp_conn.close() 49 smtp_conn = smtplib.SMTP() 50 smtp_conn.connect() 51 smtp_conn.sendmail(email_data['From'], self.suscriptors, 52 email_data.as_string()) 53 smtp_conn.close() 54 55 def run(self): 56 self.get_raw_email() 57 self.save_raw_email() 58 self.send_email() -
postman/tests/__init__.py
r1 r2 2 2 3 3 from models import * 4 from mta import * 4 5 5
Note:
See TracChangeset
for help on using the changeset viewer.