Changeset 2:108a82defd3e in mailjam for postman/mta.py


Ignore:
Timestamp:
May 15, 2012, 2:29:03 AM (12 years ago)
Author:
Francisco de Borja Lopez Rio <borja@…>
Branch:
default
Phase:
public
Message:

Fixed some bugs in the models and mta modules

Added tests for the mta module, including a dummy email that is used for
those tests.

Added an initial version of the Postman class (inside the daemon module). This
class will handle the main execution of the postman daemon, the manager
that will take care of mailing lists management.

Added the structure of folders needed to store json files associated with a given
postman instance.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • postman/mta.py

    r1 r2  
    88class Sendmail():
    99   
    10     def __init__(self, mailing_list):
     10    def __init__(self, mailing_list=None):
    1111        if not isinstance(mailing_list, MailingList):
    1212            raise ValueError(mailing_list, ' is not a valid mailing list')
    1313        self.mailing_list = mailing_list
    1414        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
    1617        self.queue = []
    1718        self.archive = self.mailing_list.config.get('archive',
    18                                                     config.storage_path)
     19                                                    config.archive_path)
    1920       
    20     def get_raw_email():
     21    def get_raw_email(self):
    2122        try:       
    22             raw_email = sys.stdin.read()
     23            self.raw_email = sys.stdin.read()
    2324        except:
    2425            raise IOError('Can not get a valid email from stdin')
    25         return raw_email
     26        return self.raw_email
    2627
    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
    2833        filename = os.path.join(self.archive,
    2934                                datetime.today().strftime('%Y%d%m%H%M%S%f'))
    3035        tmpfile = file(filename, 'w')
    31         tmpfile.write(raw_email)
     36        tmpfile.write(self.raw_email)
    3237        tmpfile.close()
    3338        self.queue.append(filename)
    3439
    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()
    4246
    43         email_data['Reply-to'] = self.reply_address
     47            email_data['Reply-to'] = self.reply_to
    4448
    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()
Note: See TracChangeset for help on using the changeset viewer.