Show
Ignore:
Timestamp:
10/26/07 05:48:32 (1 year ago)
Author:
amcgregor
Message:

TurboMail 3.0 is now usable: the smtp and debug providers are working, immediate and demand managers are good-to-go, and S/MIME signatures work, too\!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/3.0/turbomail/message.py

    r41 r52  
    44 
    55import turbomail 
     6from turbomail import release 
    67from turbomail.util import AddressList 
    7 from turbomail.release import version 
    88import re, os, email 
    99 
     
    3131        This allows you to define your own message to be delivered.""" 
    3232         
    33         def __init__(self, sender=None, recipients=[], message=None): 
     33        def __init__(self, id=None, sender=None, recipients=[], message=None): 
     34                self.id = id 
    3435                self.sender = sender 
    3536                self.recipients = recipients 
    3637                self.message = message 
     38                 
     39                if not self.id: self.id = uuid() 
     40                 
     41                super(StubMessage, self).__init__() 
    3742         
    3843        def __str__(self): 
     
    5863                 
    5964                def configget(name, key, default=None): 
    60                         pass 
     65                        return kw.get(name, turbomail.config.get(key, default)) 
    6166                 
    6267                self.date = kw.get("date", formatdate(localtime=True)) 
    6368                 
    64                 self._senders = AddressList(kw.get("sender", turbomail.config.get("mail.message.sender", None))) 
    65                 self._senders = AddressList(kw.get("senders", turbomail.config.get("mail.message.senders", self._senders))) 
    66                 self._envelope = AddressList(kw.get("envelope", turbomail.config.get("mail.message.envelope", None))) 
    67                 self._reply = AddressList(kw.get("reply", turbomail.config.get("mail.message.reply", None))) 
     69                if 'authors' in kw: kw['author'] == kw['authors'] 
     70                if 'senders' in kw: kw['sender'] == kw['senders'] 
     71                 
     72                self._author = AddressList(configget("from", "mail.message.author")) 
     73                self._sender = AddressList(configget("sender", "mail.message.sender")) 
     74                self._reply = AddressList(configget("reply", "mail.message.reply")) 
    6875                self._to = AddressList(kw.get("to", None)) 
    69                 self._cc = AddressList(kw.get("cc", turbomail.config.get("mail.message.cc", None))) 
    70                 self._bcc = AddressList(kw.get("bcc", turbomail.config.get("mail.message.bcc", None))) 
    71                 self._disposition = AddressList(kw.get("disposition", turbomail.config.get("mail.message.disposition", None))) 
    72                  
    73                 self.organization = kw.get("organization", turbomail.config.get("mail.message.organization", None)) 
    74                 self.encoding = kw.get("encoding", turbomail.config.get("mail.encoding", 'us-ascii')) 
    75                 self.priority = kw.get("priority", turbomail.config.get("mail.message.priority", None)) 
     76                self._cc = AddressList(configget("cc", "mail.message.cc")) 
     77                self._bcc = AddressList(configget("bcc", "mail.message.bcc")) 
     78                self._disposition = AddressList(configget("disposition", "mail.message.disposition")) 
     79                 
     80                self.organization = configget("organization", "mail.message.organization") 
     81                self.encoding = configget("encoding", "mail.encoding", 'us-ascii') 
     82                self.priority = configget("priority", "mail.message.priority") 
    7683                self.subject = kw.get("subject", None) 
    7784                self.plain = kw.get("plain", None) 
     
    7986                self.attachments = kw.get("attachments", []) 
    8087                self.embedded = kw.get("embedded", []) 
    81                 self.headers = kw.get("headers", turbomail.config.get("mail.message.headers", [])) 
    82                 self.tries = kw.get("tries", turbomail.config.get("mail.tries", 3)) 
     88                self.headers = configget("headers", "mail.message.headers", []) 
     89                self.tries = configget("tries", "mail.tries", 3) 
    8390                 
    8491                self._id = kw.get("id", None) 
     
    94101                return self.mime.as_string() 
    95102         
    96         def _get_sender(self): 
    97                 if self._envelope: 
    98                         return self._envelope.addresses[0] 
    99                 return self._senders.addresses[0] 
    100          
    101         sender = AddressList.protected('_senders') 
    102         senders = AddressList.protected('_senders') 
    103         envelope = AddressList.protected('_envelope') 
     103        author = AddressList.protected('_author') 
     104        authors = AddressList.protected('_author') 
     105        sender = AddressList.protected('_sender') 
     106        senders = AddressList.protected('_sender') 
    104107        reply = AddressList.protected('_reply') 
    105108        to = AddressList.protected('_to') 
     
    114117                 
    115118                return self._id 
     119         
    116120        id = property(id) 
    117121         
     122        def envelope(self): 
     123                if self.sender and self.senders != self.author: 
     124                        return AddressList(self.senders) 
     125                return AddressList(self.author) 
     126         
     127        envelope = property(envelope) 
     128         
    118129        def recipients(self): 
    119                 return [isinstance(i, tuple) and i[1] or i for i in self.to + self.cc + self.bcc] 
     130                return AddressList(self.to + self.cc + self.bcc) 
     131         
    120132        recipients = property(recipients) 
    121133         
     
    123135                """Produce the final MIME message.""" 
    124136 
    125                 assert self.senders, "You must specify a sender." 
     137                assert self.author, "You must specify an author." 
    126138                assert self.subject, "You must specify a subject." 
    127139                assert self.to or self.cc or self.bcc, "You must specify at least one recipient." 
     
    134146                 
    135147                plain = MIMEText(self._callable(self.plain).encode(self.encoding), 'plain', self.encoding) 
    136                 rich = self.rich and MIMEText(self._callable(self.rich).encode(self.encoding), 'html', self.encoding) or None 
    137  
     148                 
     149                rich = None 
     150                if self.rich: 
     151                        rich = MIMEText(self._callable(self.rich).encode(self.encoding), 'html', self.encoding) 
     152                 
    138153                def generate_mime(): 
    139154                        if not rich: return plain 
    140  
     155                         
    141156                        message = MIMEMultipart('alternative') 
    142157                        message.attach(plain) 
    143  
     158                         
    144159                        if not self.embedded: 
    145160                                message.attach(rich) 
    146  
     161                         
    147162                        else: 
    148163                                embedded = MIMEMultipart('related') 
     
    150165                                for attachment in self.embedded: embedded.attach(attachment) 
    151166                                message.attach(embedded) 
    152  
     167                         
    153168                        return message 
    154  
     169                 
    155170                message = generate_mime() 
    156  
     171                 
    157172                if self.attachments: 
    158173                        attachments = MIMEMultipart() 
     
    160175                        for attachment in self.attachments: attachments.attach(attachment) 
    161176                        message = attachments 
    162  
     177                 
     178                if hasattr(self, "process_mime_message"): 
     179                        message = self.process_mime_message(message) 
     180                 
    163181                headers = [ 
    164                                 ('Sender', self.sender), # AddressList 
    165                                 ('From', self.senders), # AddressList 
    166                                 ('Reply-To', self.reply), # AddressList 
     182                                ('Sender', self.sender), 
     183                                ('From', self.author), 
     184                                ('Reply-To', self.reply), 
    167185                                ('Subject', self.subject), 
    168186                                ('Date', self.date), 
    169                                 ('To', self.to), # AddressList 
    170                                 ('Cc', self.cc), # AddressList 
    171                                 ('Disposition-Notification-To', self.disposition), # AddressList 
     187                                ('To', self.to), 
     188                                ('Cc', self.cc), 
     189                                ('Disposition-Notification-To', self.disposition), 
    172190                                ('Organization', self.organization), 
    173191                                ('X-Priority', self.priority), 
    174                                 ('X-Mailer', "TurboMail <http://www.python-turbomail.org/>"), 
    175                                 ('X-TurboMail-Version', version), 
    176                                 ('X-TurboMail-Message-GUID', self.id), 
    177                                 ('X-TurboMail-Extensions', "manager.demand v.1.0, provider.smtp v.1.0") 
    178192                        ] 
    179  
     193                 
     194                if turbomail.config.get("mail.brand", True): 
     195                        headers.extend([ 
     196                                        ('X-Mailer', "%s <%s>" % (release.name, release.url)), 
     197                                        ('X-TurboMail-Version', release.version), 
     198                                        ('X-TurboMail-Message-GUID', self.id), 
     199                                ]) 
     200                         
     201                        if turbomail.provider: 
     202                                headers.extend([ 
     203                                                ('X-TurboMail-Provider', "%s <%s>" % (turbomail.provider.name, turbomail.provider.url)), 
     204                                                ('X-TurboMail-Provider-Version', turbomail.provider.version), 
     205                                        ]) 
     206                         
     207                        if turbomail.manager: 
     208                                headers.extend([ 
     209                                                ('X-TurboMail-Manager', "%s <%s>" % (turbomail.manager.name, turbomail.manager.url)), 
     210                                                ('X-TurboMail-Manager-Version', turbomail.manager.version), 
     211                                        ]) 
     212                 
    180213                headers.extend(self.headers) 
    181  
     214                 
    182215                for header in headers: 
    183216                        if isinstance(header, (tuple, list)): 
     
    188221                        elif isinstance(header, dict): 
    189222                                message.add_header(**header) 
    190  
     223                 
    191224                self._mime = message 
    192225                self._processed = True 
     
    194227                 
    195228                return message 
     229         
    196230        mime = property(mime) 
    197231         
     
    214248                self.attachments.append(part) 
    215249         
    216         def embed(self, file, name): 
     250        def embed(self, file, name=None): 
    217251                """Attach an on-disk image file and prepare for HTML embedding. 
    218252                 
     
    234268                if isinstance(file, (str, unicode)): 
    235269                        fp = open(file, "rb") 
     270                        name = os.path.basename(file) 
    236271                else: 
    237272                        assert name is not None, "If embedding a file-like object, you must pass a custom filename." 
    238273                        fp = file 
    239274 
    240                 part = MIMEImage(fp.read()) 
     275                part = MIMEImage(fp.read(), name=name) 
    241276                fp.close() 
    242277                 
     278                del part['Content-Disposition'] 
     279                part.add_header('Content-Disposition', 'inline', filename=name) 
    243280                part.add_header('Content-ID', '<%s>' % name) 
    244281