Skip to main content

A simple email notifier for general messages or exceptions with easy environment configuration

Project description

email_notifier

A simple email notifier for general messages or exceptions with easy environment configuration

Description

email_notifier comprises two notifier classes that send emails to a given set of recipients

GeneralNotifier

Sends a general message to a number of recipient emails.

ExceptionNotifier

Sends a nicely formatted exception alert with an appropriate stack trace to a number of recipient emails.

Main Dependencies

Getting Started

  • Install the package

    pip install email_notifier
    
  • Set the environment variables in case you wish wish to have default settings for your email notifier

  • Import the GeneralNotifier and the ExceptionNotifier classes and use accordingly

    from email_notifier import GeneralNotifier, ExceptionNotifier
    
    # Assuming the environment variables have been set
    # MAIL_SERVER_HOST=127.0.0.1
    # MAIL_SERVER_PORT=10025
    # DEFAULT_SENDER_EMAIL_ADDRESS=anne@example.com
    # DEFAULT_SENDER_EMAIL_PASSWORD=password123123
    # DEFAULT_EMAIL_SUBJECT_PREFIX="Test: "
    # DEFAULT_EMAIL_SIGNATURE="Regards<br/>Sender"
    # DEFAULT_EMAIL_RECIPIENTS=paul@example.com, albert@example.com
    
    general_notifier = GeneralNotifier()
    general_notifier.send(subject="....", body="....", recipients=["..@...com", "...@...com")
    
    # if recipients are not provided, the notifier sends to the default email recipients
    general_notifier.send(subject="....", body="....")
    
    exception_notifier = ExceptionNotifier(is_exception_in_subject=True, subject="...", salutation="...")
    # The defaults also work in this case
    # exception_notifier = ExceptionNotifier()
    
    def func():
        """Just a trial function with exception handling"""
        try:
            raise IndexError("Just raising a random exception for exhibition purposes")
        except IndexError as exp:
            # an email is sent and the python logger logs the exception
            exception_notifier.notify(exp)
    
    # When the function is called, the notifier will do the needful
    func()
    

How to test

  • Clone the repo and enter its root folder

    git clone https://github.com/sopherapps/email_notifier.git && cd email_notifier
    
  • Copy the .example.env file to .env and make appropriate edits on it

    cp .example.env .env
    
  • Create a virtual environment and activate it

    virtualenv -p /usr/bin/python3.6 env && source env/bin/activate
    
  • Install the dependencies

    pip install -r requirements.txt
    
  • Run the test command

    python -m unittest
    

Environment Configuration

The package leverages environment settings in say a .env file or set directly. The environment variables include the following.

Environment Variable What it is for Default
MAIL_SERVER_HOST The SMTP host smtp.office365.com
MAIL_SERVER_PORT The SMTP port 587
DEFAULT_SENDER_EMAIL_ADDRESS sender email address ""
DEFAULT_SENDER_EMAIL_PASSWORD sender email password ""
DEFAULT_EMAIL_SUBJECT_PREFIX subject prefix ""
DEFAULT_EMAIL_SIGNATURE email signature ""
DEFAULT_EMAIL_RECIPIENTS comma separated recipient email addresses ""

Acknowledgements

This Real Python tutorial on sending emails with python was very helpful. This Real Python tutorial on publishing packages was very helpful

License

Copyright (c) 2020 Martin Ahindura Licensed under the MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

email_notifier-0.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

email_notifier-0.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file email_notifier-0.0.1.tar.gz.

File metadata

  • Download URL: email_notifier-0.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.9

File hashes

Hashes for email_notifier-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2fd2a4af3d9806f673e2065af8e9720f005a7d7a280c1c542ae1bc59a4ac9941
MD5 c2ecc88275443d712852c68832acd6aa
BLAKE2b-256 f92cf8c7922a0f1826747daa5b015cc554ab9affb36378bbb012c25ff984cdf2

See more details on using hashes here.

File details

Details for the file email_notifier-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: email_notifier-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.9

File hashes

Hashes for email_notifier-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7014a5632feb0ca46dafb383cdf48464e1d88d82b4c45e7f59e1648a2d25e2ac
MD5 ff8670cee896b1919abde17f746fcb0f
BLAKE2b-256 ac2e1d75093c01a9019b3a0cc94d4ee1bd41ce6a1a4f8e9529e10f0bed0500e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page