Mercurial > kallithea
annotate rhodecode/lib/rcmail/message.py @ 2729:e9e7c40b4f1a beta
added deque with maxlen for py2.5 compat
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 22 Aug 2012 12:09:49 +0200 |
parents | d95ef6587bca |
children | 7e5f8c12a3fc |
rev | line source |
---|---|
1717 | 1 from rhodecode.lib.rcmail.response import MailResponse |
2 | |
3 from rhodecode.lib.rcmail.exceptions import BadHeaders | |
4 from rhodecode.lib.rcmail.exceptions import InvalidMessage | |
5 | |
2452
d95ef6587bca
fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents:
1904
diff
changeset
|
6 |
1717 | 7 class Attachment(object): |
8 """ | |
9 Encapsulates file attachment information. | |
10 | |
11 :param filename: filename of attachment | |
12 :param content_type: file mimetype | |
13 :param data: the raw file data, either as string or file obj | |
14 :param disposition: content-disposition (if any) | |
15 """ | |
16 | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
17 def __init__(self, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
18 filename=None, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
19 content_type=None, |
1717 | 20 data=None, |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
21 disposition=None): |
1717 | 22 |
23 self.filename = filename | |
24 self.content_type = content_type | |
25 self.disposition = disposition or 'attachment' | |
26 self._data = data | |
27 | |
28 @property | |
29 def data(self): | |
30 if isinstance(self._data, basestring): | |
31 return self._data | |
32 self._data = self._data.read() | |
33 return self._data | |
34 | |
35 | |
36 class Message(object): | |
37 """ | |
38 Encapsulates an email message. | |
39 | |
40 :param subject: email subject header | |
41 :param recipients: list of email addresses | |
42 :param body: plain text message | |
43 :param html: HTML message | |
44 :param sender: email sender address | |
45 :param cc: CC list | |
46 :param bcc: BCC list | |
47 :param extra_headers: dict of extra email headers | |
48 :param attachments: list of Attachment instances | |
1846
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
49 :param recipients_separator: alternative separator for any of |
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
50 'From', 'To', 'Delivered-To', 'Cc', 'Bcc' fields |
1717 | 51 """ |
52 | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
53 def __init__(self, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
54 subject=None, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
55 recipients=None, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
56 body=None, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
57 html=None, |
1717 | 58 sender=None, |
59 cc=None, | |
60 bcc=None, | |
61 extra_headers=None, | |
1846
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
62 attachments=None, |
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
63 recipients_separator="; "): |
1717 | 64 |
65 self.subject = subject or '' | |
66 self.sender = sender | |
67 self.body = body | |
68 self.html = html | |
69 | |
70 self.recipients = recipients or [] | |
71 self.attachments = attachments or [] | |
72 self.cc = cc or [] | |
73 self.bcc = bcc or [] | |
74 self.extra_headers = extra_headers or {} | |
75 | |
1846
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
76 self.recipients_separator = recipients_separator |
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
77 |
1717 | 78 @property |
79 def send_to(self): | |
80 return set(self.recipients) | set(self.bcc or ()) | set(self.cc or ()) | |
81 | |
82 def to_message(self): | |
83 """ | |
84 Returns raw email.Message instance.Validates message first. | |
85 """ | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
86 |
1717 | 87 self.validate() |
88 | |
89 return self.get_response().to_message() | |
90 | |
91 def get_response(self): | |
92 """ | |
93 Creates a Lamson MailResponse instance | |
94 """ | |
95 | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
96 response = MailResponse(Subject=self.subject, |
1717 | 97 To=self.recipients, |
98 From=self.sender, | |
99 Body=self.body, | |
1846
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
100 Html=self.html, |
349a0ca30a75
Changed default recipients separator for mails to ', '
Marcin Kuzminski <marcin@python-works.com>
parents:
1818
diff
changeset
|
101 separator=self.recipients_separator) |
1717 | 102 |
103 if self.cc: | |
104 response.base['Cc'] = self.cc | |
105 | |
106 for attachment in self.attachments: | |
107 | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
108 response.attach(attachment.filename, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
109 attachment.content_type, |
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
110 attachment.data, |
1717 | 111 attachment.disposition) |
112 | |
113 response.update(self.extra_headers) | |
114 | |
115 return response | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
116 |
1717 | 117 def is_bad_headers(self): |
118 """ | |
119 Checks for bad headers i.e. newlines in subject, sender or recipients. | |
120 """ | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
121 |
1717 | 122 headers = [self.subject, self.sender] |
123 headers += list(self.send_to) | |
124 headers += self.extra_headers.values() | |
125 | |
126 for val in headers: | |
127 for c in '\r\n': | |
128 if c in val: | |
129 return True | |
130 return False | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
131 |
1717 | 132 def validate(self): |
133 """ | |
134 Checks if message is valid and raises appropriate exception. | |
135 """ | |
136 | |
137 if not self.recipients: | |
2452
d95ef6587bca
fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents:
1904
diff
changeset
|
138 raise InvalidMessage("No recipients have been added") |
1717 | 139 |
140 if not self.body and not self.html: | |
2452
d95ef6587bca
fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents:
1904
diff
changeset
|
141 raise InvalidMessage("No body has been set") |
1717 | 142 |
143 if not self.sender: | |
2452
d95ef6587bca
fixes #481 rhodecode emails are sent without Date header
Marcin Kuzminski <marcin@python-works.com>
parents:
1904
diff
changeset
|
144 raise InvalidMessage("No sender address has been set") |
1717 | 145 |
146 if self.is_bad_headers(): | |
147 raise BadHeaders | |
148 | |
149 def add_recipient(self, recipient): | |
150 """ | |
151 Adds another recipient to the message. | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
152 |
1717 | 153 :param recipient: email address of recipient. |
154 """ | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
155 |
1717 | 156 self.recipients.append(recipient) |
157 | |
158 def add_cc(self, recipient): | |
159 """ | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
160 Adds an email address to the CC list. |
1717 | 161 |
162 :param recipient: email address of recipient. | |
163 """ | |
164 | |
165 self.cc.append(recipient) | |
166 | |
167 def add_bcc(self, recipient): | |
168 """ | |
1818
cf51bbfb120e
auto white-space removal
Marcin Kuzminski <marcin@python-works.com>
parents:
1717
diff
changeset
|
169 Adds an email address to the BCC list. |
1717 | 170 |
171 :param recipient: email address of recipient. | |
172 """ | |
173 | |
174 self.bcc.append(recipient) | |
175 | |
176 def attach(self, attachment): | |
177 """ | |
178 Adds an attachment to the message. | |
179 | |
180 :param attachment: an **Attachment** instance. | |
181 """ | |
182 | |
183 self.attachments.append(attachment) |