Changeset 52 for branches/3.0/turbomail/message.py
- Timestamp:
- 10/26/07 05:48:32 (1 year ago)
- Files:
-
- 1 modified
-
branches/3.0/turbomail/message.py (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/turbomail/message.py
r41 r52 4 4 5 5 import turbomail 6 from turbomail import release 6 7 from turbomail.util import AddressList 7 from turbomail.release import version8 8 import re, os, email 9 9 … … 31 31 This allows you to define your own message to be delivered.""" 32 32 33 def __init__(self, sender=None, recipients=[], message=None): 33 def __init__(self, id=None, sender=None, recipients=[], message=None): 34 self.id = id 34 35 self.sender = sender 35 36 self.recipients = recipients 36 37 self.message = message 38 39 if not self.id: self.id = uuid() 40 41 super(StubMessage, self).__init__() 37 42 38 43 def __str__(self): … … 58 63 59 64 def configget(name, key, default=None): 60 pass65 return kw.get(name, turbomail.config.get(key, default)) 61 66 62 67 self.date = kw.get("date", formatdate(localtime=True)) 63 68 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")) 68 75 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") 76 83 self.subject = kw.get("subject", None) 77 84 self.plain = kw.get("plain", None) … … 79 86 self.attachments = kw.get("attachments", []) 80 87 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) 83 90 84 91 self._id = kw.get("id", None) … … 94 101 return self.mime.as_string() 95 102 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') 104 107 reply = AddressList.protected('_reply') 105 108 to = AddressList.protected('_to') … … 114 117 115 118 return self._id 119 116 120 id = property(id) 117 121 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 118 129 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 120 132 recipients = property(recipients) 121 133 … … 123 135 """Produce the final MIME message.""" 124 136 125 assert self. senders, "You must specify a sender."137 assert self.author, "You must specify an author." 126 138 assert self.subject, "You must specify a subject." 127 139 assert self.to or self.cc or self.bcc, "You must specify at least one recipient." … … 134 146 135 147 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 138 153 def generate_mime(): 139 154 if not rich: return plain 140 155 141 156 message = MIMEMultipart('alternative') 142 157 message.attach(plain) 143 158 144 159 if not self.embedded: 145 160 message.attach(rich) 146 161 147 162 else: 148 163 embedded = MIMEMultipart('related') … … 150 165 for attachment in self.embedded: embedded.attach(attachment) 151 166 message.attach(embedded) 152 167 153 168 return message 154 169 155 170 message = generate_mime() 156 171 157 172 if self.attachments: 158 173 attachments = MIMEMultipart() … … 160 175 for attachment in self.attachments: attachments.attach(attachment) 161 176 message = attachments 162 177 178 if hasattr(self, "process_mime_message"): 179 message = self.process_mime_message(message) 180 163 181 headers = [ 164 ('Sender', self.sender), # AddressList165 ('From', self. senders), # AddressList166 ('Reply-To', self.reply), # AddressList182 ('Sender', self.sender), 183 ('From', self.author), 184 ('Reply-To', self.reply), 167 185 ('Subject', self.subject), 168 186 ('Date', self.date), 169 ('To', self.to), # AddressList170 ('Cc', self.cc), # AddressList171 ('Disposition-Notification-To', self.disposition), # AddressList187 ('To', self.to), 188 ('Cc', self.cc), 189 ('Disposition-Notification-To', self.disposition), 172 190 ('Organization', self.organization), 173 191 ('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")178 192 ] 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 180 213 headers.extend(self.headers) 181 214 182 215 for header in headers: 183 216 if isinstance(header, (tuple, list)): … … 188 221 elif isinstance(header, dict): 189 222 message.add_header(**header) 190 223 191 224 self._mime = message 192 225 self._processed = True … … 194 227 195 228 return message 229 196 230 mime = property(mime) 197 231 … … 214 248 self.attachments.append(part) 215 249 216 def embed(self, file, name ):250 def embed(self, file, name=None): 217 251 """Attach an on-disk image file and prepare for HTML embedding. 218 252 … … 234 268 if isinstance(file, (str, unicode)): 235 269 fp = open(file, "rb") 270 name = os.path.basename(file) 236 271 else: 237 272 assert name is not None, "If embedding a file-like object, you must pass a custom filename." 238 273 fp = file 239 274 240 part = MIMEImage(fp.read() )275 part = MIMEImage(fp.read(), name=name) 241 276 fp.close() 242 277 278 del part['Content-Disposition'] 279 part.add_header('Content-Disposition', 'inline', filename=name) 243 280 part.add_header('Content-ID', '<%s>' % name) 244 281
