20 |
20 |
21 from .Info import Program, Version, BugAddress, FeatureAddress |
21 from .Info import Program, Version, BugAddress, FeatureAddress |
22 import Preferences |
22 import Preferences |
23 import Utilities |
23 import Utilities |
24 |
24 |
|
25 ############################################################ |
|
26 ## This code is to work around a bug in the Python email ## |
|
27 ## package for Image and Audio mime messages. ## |
|
28 ############################################################ |
|
29 from base64 import b64encode as _bencode |
|
30 def _encode_base64(msg): |
|
31 """Encode the message's payload in Base64. |
|
32 |
|
33 Also, add an appropriate Content-Transfer-Encoding header. |
|
34 """ |
|
35 orig = msg.get_payload() |
|
36 encdata = str(_bencode(orig), "ASCII") |
|
37 msg.set_payload(encdata) |
|
38 msg['Content-Transfer-Encoding'] = 'base64' |
|
39 |
25 from email import encoders |
40 from email import encoders |
|
41 encoders.encode_base64 = _encode_base64 # WORK AROUND: implement our corrected encoder |
26 from email.mime.base import MIMEBase |
42 from email.mime.base import MIMEBase |
27 from email.mime.text import MIMEText |
43 from email.mime.text import MIMEText |
28 from email.mime.image import MIMEImage |
44 from email.mime.image import MIMEImage |
29 from email.mime.audio import MIMEAudio |
45 from email.mime.audio import MIMEAudio |
30 from email.mime.multipart import MIMEMultipart |
46 from email.mime.multipart import MIMEMultipart |
193 msg['Subject'] = '[eric5] %s' % self.subject.text() |
209 msg['Subject'] = '[eric5] %s' % self.subject.text() |
194 msg.preamble = mpPreamble |
210 msg.preamble = mpPreamble |
195 msg.epilogue = '' |
211 msg.epilogue = '' |
196 |
212 |
197 # second part is intended to be read |
213 # second part is intended to be read |
198 att = MIMEText(msgtext, |
214 att = MIMEText(msgtext.encode(), |
199 _charset = Preferences.getSystem("StringEncoding")) |
215 _charset = Preferences.getSystem("StringEncoding")) |
200 msg.attach(att) |
216 msg.attach(att) |
201 |
217 |
202 # next parts contain the attachments |
218 # next parts contain the attachments |
203 for index in range(self.attachments.topLevelItemCount()): |
219 for index in range(self.attachments.topLevelItemCount()): |
204 itm = self.attachments.topLevelItem(index) |
220 itm = self.attachments.topLevelItem(index) |
205 maintype, subtype = str(itm.text(1)).split('/', 1) |
221 maintype, subtype = itm.text(1).split('/', 1) |
206 fname = itm.text(0) |
222 fname = itm.text(0) |
207 name = os.path.basename(fname) |
223 name = os.path.basename(fname) |
208 |
224 |
209 if maintype == 'text': |
225 if maintype == 'text': |
210 att = MIMEText(open(fname, 'rb').read(), _subtype = subtype) |
226 att = MIMEText(open(fname, 'r').read(), _subtype = subtype) |
211 elif maintype == 'image': |
227 elif maintype == 'image': |
212 att = MIMEImage(open(fname, 'rb').read(), _subtype = subtype) |
228 att = MIMEImage(open(fname, 'rb').read(), _subtype = subtype) |
213 elif maintype == 'audio': |
229 elif maintype == 'audio': |
214 att = MIMEAudio(open(fname, 'rb').read(), _subtype = subtype) |
230 att = MIMEAudio(open(fname, 'rb').read(), _subtype = subtype) |
215 else: |
231 else: |
227 |
243 |
228 @param msg the message to be sent (string) |
244 @param msg the message to be sent (string) |
229 @return flag indicating success (boolean) |
245 @return flag indicating success (boolean) |
230 """ |
246 """ |
231 try: |
247 try: |
232 server = smtplib.SMTP(str(Preferences.getUser("MailServer")), |
248 server = smtplib.SMTP(Preferences.getUser("MailServer"), |
233 Preferences.getUser("MailServerPort")) |
249 Preferences.getUser("MailServerPort")) |
234 if Preferences.getUser("MailServerUseTLS"): |
250 if Preferences.getUser("MailServerUseTLS"): |
235 server.starttls() |
251 server.starttls() |
236 if Preferences.getUser("MailServerAuthentication"): |
252 if Preferences.getUser("MailServerAuthentication"): |
237 # mail server needs authentication |
253 # mail server needs authentication |