def handler(exc=None, passthru=True): if exc is None: exc = sys.exc_info() TEXT = _format_exc(exc) try: _thread = _currentThread() TEXT = ("Exception in thread %s:\n" % _thread.name) + TEXT except: pass CMD = len(sys.argv) > 1 and sys.argv[1].endswith('.py') and sys.argv[1] or sys.argv[0] SUBJECT = CMD+': '+str(exc[1]).replace('\r','').replace('\n','') HEADERS = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n' % (EMAIL_FROM, EMAIL_TO, SUBJECT) _SMTP('localhost').sendmail(EMAIL_FROM, [EMAIL_TO], HEADERS+TEXT) if passthru: sys.__excepthook__(*exc) return TEXT
def send(self): ''' :returns: A dictionary with the following: * 'sender': The `sender` * 'recipients': All recipients, compiled from `to`, `cc` and `bcc` * 'result': The :py:meth:`smtplib.SMTP.sendmail`-result * 'exception': The exception message (if any) .. note:: You need to have a postfix/sendmail running and listening on localhost. ''' res = dict(sender=self.__sender, recipients=self.__recipients) try: s = _SMTP() s.connect('localhost') res.update(dict( result=s.sendmail(self.__sender, self.__recipients, self.text) )) self.m('mail sent', more=res) except (_SMTPException, _error) as ex: res.update(dict( exception=str(ex) )) self.m('error sending mail', verbose=True, more=res) return res
def send(self): ''' :returns: A dictionary with the following: * 'sender': The `sender` * 'recipients': All recipients, compiled from `to`, `cc` and `bcc` * 'result': The :py:meth:`smtplib.SMTP.sendmail`-result * 'exception': The exception message (if any) .. note:: You need to have a postfix/sendmail running and listening on localhost. ''' res = dict(sender=self.__sender, recipients=self.__recipients) try: s = _SMTP() s.connect('localhost') res.update( dict(result=s.sendmail(self.__sender, self.__recipients, self.text))) self.m('mail sent', more=res) except (_SMTPException, _error) as ex: res.update(dict(exception=str(ex))) self.m('error sending mail', verbose=True, more=res) return res
def __login(self): context = _ssl.create_default_context() self.__server = _SMTP(host=self.provider, port=self.__port) self.__server.ehlo() self.__server.starttls(context=context) self.__server.ehlo() try: self.__server.login(user=self.email, password=self.__password) self.login = True return True except Exception as e: if self.provider == self.GMAIL: problem = """ Error: Email And Password Not Accepted. Note: Make sure you Allowed less secure apps, if you didn't, visit this link: ==> https://myaccount.google.com/lesssecureapps For More information visit this link: ==> https://support.google.com/mail/?p=BadCredentials """ else: problem = e print(problem) self.login = False return False
def do_send(msg: Message): try: engine = _SMTP('localhost') engine.sendmail(msg._from_addr, msg._to_addrs, str(msg)) log_msg = "Message '{}' has been sent to {}.".format(msg.subject, msg.to_addrs) _logger.info(log_msg) except Exception as e: _logger.error('Unable to send message to {}: {}'.format(msg.to_addrs, e), exc_info=e)
def email(to, subject, body): """Sends email, doing all the (not so) heavy lifting.""" try: s = _SMTP('localhost') msg = _MIMEText(body) msg['from'] = 'Driver Mail Subsystem <noreply@%s>' % _getfqdn() msg['to'] = to msg['subject'] = subject s.sendmail(msg['from'], msg['to'], msg.as_string()) s.quit() logger.info('Sent mail to %s with subject %s.', msg['to'], msg['subject']) except Exception as e: logger.exception(e)
def SMTP(*args, **kwargs): start = time.time() error = True while error and time.time() - start <= 15: try: s = _SMTP(*args, **kwargs) except ConnectionRefusedError: continue else: error = False if error: raise ConnectionRefusedError return s