def send_standard_email(self, msg): use_ssl = int(self.settings.get('mail_use_ssl', 0)) timeout = 600 # set timeout to 5mins # redirect output to logfile on python2 on python3 debugoutput is caught with overwritten # _print_debug function if sys.version_info < (3, 0): org_smtpstderr = smtplib.stderr smtplib.stderr = logger.StderrLogger('worker.smtp') if use_ssl == 2: self.asyncSMTP = EmailSSL(self.settings["mail_server"], self.settings["mail_port"], timeout=timeout) else: self.asyncSMTP = Email(self.settings["mail_server"], self.settings["mail_port"], timeout=timeout) # link to logginglevel if logger.is_debug_enabled(): self.asyncSMTP.set_debuglevel(1) if use_ssl == 1: self.asyncSMTP.starttls() if self.settings["mail_password"]: self.asyncSMTP.login(str(self.settings["mail_login"]), str(self.settings["mail_password"])) # Convert message to something to send fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) self.asyncSMTP.sendmail(self.settings["mail_from"], self.recipent, fp.getvalue()) self.asyncSMTP.quit() self._handleSuccess() if sys.version_info < (3, 0): smtplib.stderr = org_smtpstderr
def run(self, worker_thread): # create MIME message msg = MIMEMultipart() msg['Subject'] = self.subject msg['Message-Id'] = make_msgid('calibre-web') msg['Date'] = formatdate(localtime=True) text = self.text msg.attach(MIMEText(text.encode('UTF-8'), 'plain', 'UTF-8')) if self.attachment: result = self._get_attachment(self.filepath, self.attachment) if result: msg.attach(result) else: self._handleError(u"Attachment not found") return msg['From'] = self.settings["mail_from"] msg['To'] = self.recipent use_ssl = int(self.settings.get('mail_use_ssl', 0)) try: # convert MIME message to string fp = StringIO() gen = Generator(fp, mangle_from_=False) gen.flatten(msg) msg = fp.getvalue() # send email timeout = 600 # set timeout to 5mins # redirect output to logfile on python2 pn python3 debugoutput is caught with overwritten # _print_debug function if sys.version_info < (3, 0): org_smtpstderr = smtplib.stderr smtplib.stderr = logger.StderrLogger('worker.smtp') if use_ssl == 2: self.asyncSMTP = EmailSSL(self.settings["mail_server"], self.settings["mail_port"], timeout=timeout) else: self.asyncSMTP = Email(self.settings["mail_server"], self.settings["mail_port"], timeout=timeout) # link to logginglevel if logger.is_debug_enabled(): self.asyncSMTP.set_debuglevel(1) if use_ssl == 1: self.asyncSMTP.starttls() if self.settings["mail_password"]: self.asyncSMTP.login(str(self.settings["mail_login"]), str(self.settings["mail_password"])) self.asyncSMTP.sendmail(self.settings["mail_from"], self.recipent, msg) self.asyncSMTP.quit() self._handleSuccess() if sys.version_info < (3, 0): smtplib.stderr = org_smtpstderr except (MemoryError) as e: log.exception(e) self._handleError(u'MemoryError sending email: ' + str(e)) # return None except (smtplib.SMTPException, smtplib.SMTPAuthenticationError) as e: if hasattr(e, "smtp_error"): text = e.smtp_error.decode('utf-8').replace("\n", '. ') elif hasattr(e, "message"): text = e.message elif hasattr(e, "args"): text = '\n'.join(e.args) else: log.exception(e) text = '' self._handleError(u'Smtplib Error sending email: ' + text) # return None except (socket.error) as e: self._handleError(u'Socket Error sending email: ' + e.strerror)