Skip to content

A utility class to simplify sending emails from within python packages.

License

Notifications You must be signed in to change notification settings

Stephen-RA-King/pynnacle

Repository files navigation

Pynnacle

A utility class to simplify sending emails.

PyPI Downloads Status Python Version Format

pre-commit pre-commit.ci status CodeFactor Codeclimate CodeQl readthedocs Imports: isort Code style: black Checked with mypy security: bandit Commitizen friendly Conventional Commits DeepSource license

Pynnacle provides a wrapper to mimetypes, smtplib and email.message libraries to provide a simplified facade interface to make sending emails as simple as possible. It abstracts away all the low level details and when imported into other modules provides a clean, clutter-free interface.

Installation


OS X & Linux:

pip3 install pynnacle

Windows:

pip install pynnacle

Usage example


Firstly import the module

from pynnacle.pynnacle import SendEmail

Pynnacle stores the configuration of email servers in an 'ini' configuration file. If a service is already configured then the main class can be instantiated with only 3 arguments e.g.:

mailer = SendEmail(
    service="gmail",
    user_id="jsmith",
    user_pass="P@zzw0rd1",
)

If the service has not been configured, simply pass "custom" as the service and pass the other smtp arguments to the initializer e.g.:

mailer = SendEmail(
    service="custom",
    user_id="jsmith",
    user_pass="P@zzw0rd1",
    smtp_server="smtp.abc.com",
    smtp_port=25,
    smtp_authentication="yes",
    smtp_encryption="yes",
)

Then simply send the email

mailer.message_send(
    subject="Hi There",
    sender="sender@abc.com",
    recipient="recipient@xyz.com",
    body="This is where the text of the email body goes",
)

cc, bcc and attachments arguments can also be used, supplied as lists

mailer.message_send(
    subject="Hi There",
    sender="sender@abc.com",
    recipient="recipient@xyz.com",
    body="This is where the text of the email body goes",
    cc=["person1@def.com", "person2@ghi.com"],
    bcc=["person3@jkl.com", "person4@mno.com"],
    attachments=["path_to_file1", "path_to_file2"]
)

Further simplifications


Storing and Reusing SMTP

Additional setting can be saved in the "ini" file as and when you like.

e.g.config.ini

[gmail]
smtp_server = smtp.gmail.com
smtp_port = 587
smtp_authentication = yes
smtp_encryption = yes
pop3_server = pop.gmail.com
pop3_port = 995
pop3_authentication = yes
pop3_encryption = yes

Storing credentials

To avoid hard-coding any credentials I use the Python keyring library

service = "gmail"

user_id = keyring.get_password(service, "service_id")
user_pass = keyring.get_password(service, "service_password")

For more examples and usage, please refer to the Wiki.

A Note on gmail authentication


As of 30/05/2022 Google will no longer support the use of third-party apps or devices that only ask for your username and password. The "Less secure app access" setting has now been turned off. The application now has to be assigned a 16 byte code which can be configured from your account as follows:

  • 1 Log onto your account: https://myaccount.google.com
  • 2 Goto security
  • 3 Enable 2-step verification
  • 4 click "App password" to generate the key

Then simply use this along with the account email address to authenticate

Documentation


Read the Docs

Wiki

Meta


Stephen R A King : sking.github@gmail.com

Distributed under the MIT license. See for more information.

Created with Cookiecutter template: pydough version 1.2.1