Skip to main content

A email-tool package simplified sending email.

Project description

emailtools-python3

A simple wrapper for standard lib -- email & stmplib

Simplify the steps of sending emails in Python, generate and send neatly formatted emails with just a few lines of code.

A simple example:

from PIL import Image
from emailtools import Email, EmailServer

img = Image.open("the_email_path")
mail = Email.sequence(
    "The subject",
    "# header-1",
    "paragraph", 
    "another paragraph",
    img,
    "# header-2",
    "another paragraph",
)
server = EmailServer.initFromJson("./emailserver.json")
server.send(mail)

The above code can be used to send emails. The content of the message can have headers, text, and tables. If pillow and pandas avaliable, image and DataFrame can add to mail.

config-file

Config file "./emailserver.json" (Of course you can use any name you like) contains mail-server-address, sender name, sender account and default receivers(optional). Format of config file is below:

{
    "host": "smtp.xxx.com",
    "addr": "email_address@xxx.com",
    "name": "your_nick_name",
    "key": "password",
    "receivers": {
        "reveiver_1_address@xx.com": "his/her_nickname"
    }
}

Install

Install with pip :

> pip install tl-emailtools -y

Dependency

  • No essential dependency when running on Python3.12 In this case, only text and table can add to mail
  • pillow needed when sending image
  • pandas needed when sending DataFrame

How to use

  1. Create Email instance. Email provides two ways to create a mail:
mail_subject = "theSubjectOfMail"

# By instantiating `Email` class
mail = Email(mail_subject)

# By class method
mail = Email.sequence(mail_subject)

Subject of mail is needed when creating mail.

  1. Add content to mail by mail.add* methods.
# add header
mail.addHead("header1")             # h1 (default)
mail.addHead("header2", level=1)    # h1
mail.addHead("header3", level=2)    # h2

# add paragraph
mail.addParagraph("This is a paragraph.")

# add any text
# start with one '#' or more would be seemed as header
mail.addText("# header4")                   # h1
mail.addText("This is another paragraph.")  # paragraph

# add table (2d)
t = [ [1,2,3], [4,5,6] ]    
th = ["A", "B", "C"]        
tc = "table_caption"
mail.addTable(t, head=th, caption=tc) # head ana caption is optional

# add Image (avaliable when `pillow`)
from PIL import Image
img = Image.open("any.png")
mail.addImage(img)

# add DataFrame (avaliable when `pandas`)
import pandas as pd
df = pd.DataFrame(
    [ [1, 2, 3], [4, 5, 6] ], 
    columns=["A", "B", "C"]
)
mail.addDataFrame(df)

More convenient, use classmethod Email.sequence() to create mail with any number of content:

mail = Email.sequence(
    "The subject",          # Subject (essential)
    "# header-1",           # h1
    "paragraph",            # p
    "another paragraph",    # p
    img,                    # image
    "## header-2",          # h2
    "another paragraph",    # p
)
  1. Init EmailServer . Specific your account, passwd and mail-server-address:

Cation: DO NOT write your account in public code

# use parameter
server = EmailServer(
    "smtp.xxx.com",             # address
    "local_nickname",           # local name
    "local_address@xmail.com",  # local account
    "xxxxxxxxxxxxxxxxxxx",      # local key
)

# use config file
server = EmailServer.initFromJson("cfg.json")

Format of config file see this.

  1. Send the mail
# send the mail
server.send(mail)

server will retain logging in. Auto log out when instance deleted. Manually log out is not needed.


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

tl_emailtools-1.1.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tl_emailtools-1.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file tl_emailtools-1.1.1.tar.gz.

File metadata

  • Download URL: tl_emailtools-1.1.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tl_emailtools-1.1.1.tar.gz
Algorithm Hash digest
SHA256 6932b2adba586428b9d8076b6c9fc46191146d64d9186702ffd403c9995436c7
MD5 381d5278b9bbe9e402d629ef6440bebd
BLAKE2b-256 90fab4e826f5300c175d66c38967180ccc13d1100bb6dc0c9e71dd118be45e0f

See more details on using hashes here.

File details

Details for the file tl_emailtools-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: tl_emailtools-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tl_emailtools-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4210e965e65a78c01e3b90b7623704cc5f96ddcf64b646752659229c456df767
MD5 c25a202a4ec6c3785bd3d65f8367a496
BLAKE2b-256 8cdf017d205688a21c9feac604d2bc175c9687efe4b90e0faa8f4e10ac44e137

See more details on using hashes here.

Supported by

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