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"
    }
}

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.0.tar.gz (7.4 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.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tl_emailtools-1.1.0.tar.gz
  • Upload date:
  • Size: 7.4 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.0.tar.gz
Algorithm Hash digest
SHA256 a8c5a6c6ee068b37f62a45708a3643839a3113e39e6e51123d1c12e5b5547ebb
MD5 833d7c49f70f6b4d565d7e362f996891
BLAKE2b-256 651bf38924018e85dc0a85780582739333b7e1ee1bf4cfee055ab78d35b7f35a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tl_emailtools-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56830fbdcea6b5aa0c83770a32a12f657fd001a493c18731f5be52cf9506c9a8
MD5 52c482b0353d8732825495b9e92f2564
BLAKE2b-256 bf68f55ca61c0a720a7c4977fce2975ba2b411958bb1a632d31c808a3b282d06

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