Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """
- Just import this module and change the settings.
- Uncaught Exceptions were sent via E-Mail with a thread.
- The emails package is required.
- """
- import sys
- import traceback
- from pathlib import Path
- from threading import Thread
- try:
- from emails import Message
- except ImportError:
- raise SystemExit("Please install the emails package\n\npip3 install emails")
- class Sender:
- def __init__(
- self, mail_from, mail_to, username, password, host, port, tls, timeout
- ):
- self.smtp = {
- "user": username,
- "password": password,
- "host": host,
- "port": port,
- "timeout": timeout,
- "tls": tls,
- }
- self.mail_to = mail_to
- self.mail_from = mail_from
- def send(self, subject, text):
- Thread(target=self._send, args=(subject, text)).start()
- def _send(self, subject, text):
- return (
- Message(
- subject=subject,
- mail_from=self.mail_from,
- mail_to=self.mail_to,
- text=text,
- )
- .send(smtp=self.smtp)
- .error
- )
- def hook(exctype, value, tb):
- tb = "\n".join(traceback.format_exception(exctype, value, tb))
- _sender.send(f"Exception: {exctype}", f"Value: {value}\nTraceback: {tb}\n")
- MAIL_FROM = "mail@domain.tld"
- MAIL_TO = "mail@domain.tld"
- HOST = "smtp.domain.tld"
- PORT = 587
- TLS = True
- TIMEOUT = 5
- USERNAME = "mail@domain.tld"
- # Password could be stored in a file or you put it into the source,
- # which is not a good idea
- PASSWORD = Path.home().joinpath(".config/.emailpw").read_text().rstrip()
- # make an instance of Sender
- _sender = Sender(MAIL_FROM, MAIL_TO, USERNAME, PASSWORD, HOST, PORT, TLS, TIMEOUT)
- # replacing the sys.excpthook, which is called, if an uncaught exception occurs.
- # the function hook is the replacement
- sys.excepthook = hook
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement