155 os.remove(f) |
155 os.remove(f) |
156 except OSError: |
156 except OSError: |
157 pass |
157 pass |
158 self.accept() |
158 self.accept() |
159 |
159 |
|
160 def __encodedText(self, txt): |
|
161 """ |
|
162 Private method to create a MIMEText message with correct encoding. |
|
163 |
|
164 @param txt text to be put into the MIMEText object (string) |
|
165 @return MIMEText object |
|
166 """ |
|
167 try: |
|
168 txt.encode("us-ascii") |
|
169 return MIMEText(txt) |
|
170 except UnicodeEncodeError: |
|
171 coding = Preferences.getSystem("StringEncoding") |
|
172 return MIMEText(txt.encode(coding), _charset = coding) |
|
173 |
|
174 def __encodedHeader(self, txt): |
|
175 """ |
|
176 Private method to create a correctly encoded mail header. |
|
177 |
|
178 @param txt header text to encode (string) |
|
179 @return encoded header (email.header.Header) |
|
180 """ |
|
181 try: |
|
182 txt.encode("us-ascii") |
|
183 return Header(txt) |
|
184 except UnicodeEncodeError: |
|
185 coding = Preferences.getSystem("StringEncoding") |
|
186 return Header(txt, coding) |
|
187 |
160 def __createSimpleMail(self): |
188 def __createSimpleMail(self): |
161 """ |
189 """ |
162 Private method to create a simple mail message. |
190 Private method to create a simple mail message. |
163 |
191 |
164 @return string containing the mail message |
192 @return string containing the mail message |
165 """ |
193 """ |
166 coding = Preferences.getSystem("StringEncoding") |
|
167 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ |
194 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ |
168 (self.message.toPlainText(), |
195 (self.message.toPlainText(), |
169 Utilities.generateVersionInfo("\r\n"), |
196 Utilities.generateVersionInfo("\r\n"), |
170 Utilities.generatePluginsVersionInfo("\r\n"), |
197 Utilities.generatePluginsVersionInfo("\r\n"), |
171 Utilities.generateDistroInfo("\r\n")) |
198 Utilities.generateDistroInfo("\r\n")) |
172 |
199 |
173 try: |
200 msg = self.__encodedText(msgtext) |
174 msgtext.encode("us-ascii") |
|
175 msg = MIMEText(msgtext) |
|
176 except UnicodeEncodeError: |
|
177 msg = MIMEText(msgtext.encode(coding), _charset = coding) |
|
178 msg['From'] = Preferences.getUser("Email") |
201 msg['From'] = Preferences.getUser("Email") |
179 msg['To'] = self.__toAddress |
202 msg['To'] = self.__toAddress |
180 subject = '[eric5] %s' % self.subject.text() |
203 subject = '[eric5] %s' % self.subject.text() |
181 try: |
204 msg['Subject'] = self.__encodedHeader(subject) |
182 subject.encode("us-ascii") |
|
183 msg['Subject'] = subject |
|
184 except UnicodeEncodeError: |
|
185 msg['Subject'] = Header(subject, coding) |
|
186 |
205 |
187 return msg.as_string() |
206 return msg.as_string() |
188 |
207 |
189 def __createMultipartMail(self): |
208 def __createMultipartMail(self): |
190 """ |
209 """ |
191 Private method to create a multipart mail message. |
210 Private method to create a multipart mail message. |
192 |
211 |
193 @return string containing the mail message |
212 @return string containing the mail message |
194 """ |
213 """ |
195 coding = Preferences.getSystem("StringEncoding") |
|
196 mpPreamble = ("This is a MIME-encoded message with attachments. " |
214 mpPreamble = ("This is a MIME-encoded message with attachments. " |
197 "If you see this message, your mail client is not " |
215 "If you see this message, your mail client is not " |
198 "capable of displaying the attachments.") |
216 "capable of displaying the attachments.") |
199 |
217 |
200 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ |
218 msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \ |
206 # first part of multipart mail explains format |
224 # first part of multipart mail explains format |
207 msg = MIMEMultipart() |
225 msg = MIMEMultipart() |
208 msg['From'] = Preferences.getUser("Email") |
226 msg['From'] = Preferences.getUser("Email") |
209 msg['To'] = self.__toAddress |
227 msg['To'] = self.__toAddress |
210 subject = '[eric5] %s' % self.subject.text() |
228 subject = '[eric5] %s' % self.subject.text() |
211 try: |
229 msg['Subject'] = self.__encodedHeader(subject) |
212 subject.encode("us-ascii") |
|
213 msg['Subject'] = subject |
|
214 except UnicodeEncodeError: |
|
215 msg['Subject'] = Header(subject, coding) |
|
216 msg.preamble = mpPreamble |
230 msg.preamble = mpPreamble |
217 msg.epilogue = '' |
231 msg.epilogue = '' |
218 |
232 |
219 # second part is intended to be read |
233 # second part is intended to be read |
220 try: |
234 att = self.__encodedText(msgtext) |
221 msgtext.encode("us-ascii") |
|
222 att = MIMEText(msgtext) |
|
223 except UnicodeEncodeError: |
|
224 att = MIMEText(msgtext.encode(coding), _charset = coding) |
|
225 msg.attach(att) |
235 msg.attach(att) |
226 |
236 |
227 # next parts contain the attachments |
237 # next parts contain the attachments |
228 for index in range(self.attachments.topLevelItemCount()): |
238 for index in range(self.attachments.topLevelItemCount()): |
229 itm = self.attachments.topLevelItem(index) |
239 itm = self.attachments.topLevelItem(index) |