UI/EmailDialog.py

changeset 119
071ac17b8179
parent 97
c4086afea02b
child 120
c0ef4b5e679a
equal deleted inserted replaced
118:8be7b777742c 119:071ac17b8179
39 msg.set_payload(encdata) 39 msg.set_payload(encdata)
40 msg['Content-Transfer-Encoding'] = 'base64' 40 msg['Content-Transfer-Encoding'] = 'base64'
41 41
42 from email import encoders 42 from email import encoders
43 encoders.encode_base64 = _encode_base64 # WORK AROUND: implement our corrected encoder 43 encoders.encode_base64 = _encode_base64 # WORK AROUND: implement our corrected encoder
44 from email.mime.base import MIMEBase
45 from email.mime.text import MIMEText 44 from email.mime.text import MIMEText
46 from email.mime.image import MIMEImage 45 from email.mime.image import MIMEImage
47 from email.mime.audio import MIMEAudio 46 from email.mime.audio import MIMEAudio
47 from email.mime.application import MIMEApplication
48 from email.mime.multipart import MIMEMultipart 48 from email.mime.multipart import MIMEMultipart
49 from email.header import Header
49 50
50 class EmailDialog(QDialog, Ui_EmailDialog): 51 class EmailDialog(QDialog, Ui_EmailDialog):
51 """ 52 """
52 Class implementing a dialog to send bug reports. 53 Class implementing a dialog to send bug reports.
53 """ 54 """
160 """ 161 """
161 Private method to create a simple mail message. 162 Private method to create a simple mail message.
162 163
163 @return string containing the mail message 164 @return string containing the mail message
164 """ 165 """
166 coding = Preferences.getSystem("StringEncoding")
165 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ 167 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \
166 (self.message.toPlainText(), 168 (self.message.toPlainText(),
167 Utilities.generateVersionInfo("\r\n"), 169 Utilities.generateVersionInfo("\r\n"),
168 Utilities.generatePluginsVersionInfo("\r\n"), 170 Utilities.generatePluginsVersionInfo("\r\n"),
169 Utilities.generateDistroInfo("\r\n")) 171 Utilities.generateDistroInfo("\r\n"))
170 172
171 msg = MIMEText(msgtext, 173 try:
172 _charset = Preferences.getSystem("StringEncoding")) 174 msgtext.encode("us-ascii")
175 msg = MIMEText(msgtext)
176 except UnicodeEncodeError:
177 msg = MIMEText(msgtext.encode(coding), _charset = coding)
173 msg['From'] = Preferences.getUser("Email") 178 msg['From'] = Preferences.getUser("Email")
174 msg['To'] = self.__toAddress 179 msg['To'] = self.__toAddress
175 msg['Subject'] = '[eric5] %s' % self.subject.text() 180 subject = '[eric5] %s' % self.subject.text()
176 181 try:
182 subject.encode("us-ascii")
183 msg['Subject'] = subject
184 except UnicodeEncodeError:
185 msg['Subject'] = Header(subject, coding)
186
177 return msg.as_string() 187 return msg.as_string()
178 188
179 def __createMultipartMail(self): 189 def __createMultipartMail(self):
180 """ 190 """
181 Private method to create a multipart mail message. 191 Private method to create a multipart mail message.
182 192
183 @return string containing the mail message 193 @return string containing the mail message
184 """ 194 """
195 coding = Preferences.getSystem("StringEncoding")
185 mpPreamble = ("This is a MIME-encoded message with attachments. " 196 mpPreamble = ("This is a MIME-encoded message with attachments. "
186 "If you see this message, your mail client is not " 197 "If you see this message, your mail client is not "
187 "capable of displaying the attachments.") 198 "capable of displaying the attachments.")
188 199
189 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ 200 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \
194 205
195 # first part of multipart mail explains format 206 # first part of multipart mail explains format
196 msg = MIMEMultipart() 207 msg = MIMEMultipart()
197 msg['From'] = Preferences.getUser("Email") 208 msg['From'] = Preferences.getUser("Email")
198 msg['To'] = self.__toAddress 209 msg['To'] = self.__toAddress
199 msg['Subject'] = '[eric5] %s' % self.subject.text() 210 subject = '[eric5] %s' % self.subject.text()
211 try:
212 subject.encode("us-ascii")
213 msg['Subject'] = subject
214 except UnicodeEncodeError:
215 msg['Subject'] = Header(subject, coding)
200 msg.preamble = mpPreamble 216 msg.preamble = mpPreamble
201 msg.epilogue = '' 217 msg.epilogue = ''
202 218
203 # second part is intended to be read 219 # second part is intended to be read
204 att = MIMEText(msgtext.encode(), 220 try:
205 _charset = Preferences.getSystem("StringEncoding")) 221 msgtext.encode("us-ascii")
222 att = MIMEText(msgtext)
223 except UnicodeEncodeError:
224 att = MIMEText(msgtext.encode(coding), _charset = coding)
206 msg.attach(att) 225 msg.attach(att)
207 226
208 # next parts contain the attachments 227 # next parts contain the attachments
209 for index in range(self.attachments.topLevelItemCount()): 228 for index in range(self.attachments.topLevelItemCount()):
210 itm = self.attachments.topLevelItem(index) 229 itm = self.attachments.topLevelItem(index)
211 maintype, subtype = itm.text(1).split('/', 1) 230 maintype, subtype = itm.text(1).split('/', 1)
212 fname = itm.text(0) 231 fname = itm.text(0)
213 name = os.path.basename(fname) 232 name = os.path.basename(fname)
214 233
215 if maintype == 'text': 234 if maintype == 'text':
216 att = MIMEText( 235 txt = open(fname, 'r', encoding = "utf-8").read()
217 open(fname, 'r', encoding = "utf-8").read(), _subtype = subtype) 236 try:
237 txt.encode("us-ascii")
238 att = MIMEText(txt, _subtype = subtype)
239 except UnicodeEncodeError:
240 att = MIMEText(
241 txt.encode("utf-8"), _subtype = subtype, _charset = "utf-8")
218 elif maintype == 'image': 242 elif maintype == 'image':
219 att = MIMEImage(open(fname, 'rb').read(), _subtype = subtype) 243 att = MIMEImage(open(fname, 'rb').read(), _subtype = subtype)
220 elif maintype == 'audio': 244 elif maintype == 'audio':
221 att = MIMEAudio(open(fname, 'rb').read(), _subtype = subtype) 245 att = MIMEAudio(open(fname, 'rb').read(), _subtype = subtype)
222 else: 246 else:
223 att = MIMEBase(maintype, subtype) 247 att = MIMEApplication(open(fname, 'rb').read())
224 att.set_payload(open(fname, 'rb').read())
225 encoders.encode_base64(att)
226 att.add_header('Content-Disposition', 'attachment', filename = name) 248 att.add_header('Content-Disposition', 'attachment', filename = name)
227 msg.attach(att) 249 msg.attach(att)
228 250
229 return msg.as_string() 251 return msg.as_string()
230 252

eric ide

mercurial