Skip to content

Souleymane-T/concrete-mailer

 
 

Repository files navigation

README

Code Quality

GitHub Workflow Status GitHub Workflow Status GitHub Workflow Status GitHub Workflow Status Codecov Code Style Python Python Python Python

Description

concrete-mailer is a python package for sending rich e-mails.

  • It automatically embeds the linked image in email inline attachments.
  • It avoids emails to end up in SPAM (applying best practices, with no guarantee)
  • It automatically converts rich e-mails in plain-text format for receivers who have not enabled HTML format
  • It allows sending e-mails with attachements
  • It inlines CSS (no header in HTML e-mails)

warning It requires an SMTP server.

Setup the environment

pip install concrete-mailer

User Guide

concrete-mailer can be used in two ways:

1- Email Client (client.py)

from concrete_mailer.client import EmailSenderClient
import os

#:  Define the smtp connexion variables in:
#:  SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_HOST_USER, SMTP_HOST_PASSWORD
client = EmailSenderClient(
    email_host=os.getenv('SMTP_HOST_NAME'),  #: smtp hostname
    email_port=os.getenv('SMTP_HOST_PORT'),  #: smtp host port
    email_host_user=os.getenv('SMTP_HOST_USER'),  #: smtp host username
    email_host_password=os.getenv('SMTP_HOST_PASSWORD'),  #: smtp host password
    use_tls=os.getenv('SMTP_USE_TLS') == '1',  #: smtp use tls
)

for name, email in (('John', '[email protected]'), ('Jane', '[email protected]')):
    client.send(
        context={'name': name},
        template='''
            <h1>Concrete Mailer</h1>
            <p>Hello {{name}}. Welcome to README</p>
            <img src="/path/to/image1.jpg">
        ''',
        title='README',
        dests=[email, '[email protected]'],
        sender_name='Netsach',
        sender_email='[email protected]',
        reply_to='[email protected]',
        attachments=['/path/to/file.zip', '/path/to/image2.png'],
    )

send method returns:

  • True if the email is successfully sent.
  • False if a problem has occured.

2- Email preparers (preparer.py)

from concrete_mailer.preparers import prepare_email
import os

#:  Define the smtp connexion variables in:
#:  SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_HOST_USER, SMTP_HOST_PASSWORD
for name, email in (('John', '[email protected]'), ('Jane', '[email protected]')):
    email = prepare_email(
        context={'name': name},
        html='''
            <h1>Concrete Mailer</h1>
            <p>Hello {{name}}. Welcome to README</p>
            <img src="/path/to/image1.jpg">
        ''',
        title='README',
        css='',  #: extra css
        sender='Netsach <[email protected]>',
        reply_to='[email protected]',
        recipients=[email, '[email protected]'],
        attachments=['/path/to/file.zip', '/path/to/image2.png'],
        email_host=os.getenv('SMTP_HOST_NAME'),  #: smtp hostname
        email_port=os.getenv('SMTP_HOST_PORT'),  #: smtp host port
        email_host_user=os.getenv('SMTP_HOST_USER'),  #: smtp host username
        email_host_password=os.getenv('SMTP_HOST_PASSWORD'),  #: smtp host password
        use_tls=os.getenv('SMTP_USE_TLS') == '1',  #: smtp use tls
    )
    email.send()

send method returns:

  • True if the email is successfully sent.
  • False if a problem has occured.

Debug

Python standard package includes a smtpd module.

According to the official documentation

This module offers several classes to implement SMTP (email) servers.

One of thoses classes is DebuggingServer.

Create a new debugging server. Arguments are as per SMTPServer. Messages will be discarded, and printed on stdout.

If you want to test your emails, open a new console and invoke the following command:

python3 -m smtpd -n -c DebuggingServer localhost:1025

and configure your environment :

SMTP_HOST_NAME='localhost'
SMTP_HOST_PORT='1025'
SMTP_HOST_USER=''
SMTP_HOST_PASSWORD=''
SMTP_USE_TLS=''

The email body will be displayed in console instead of being sent to destinations. Only local stmp connection will be established (dry-run)

About

Python package for sending rich e-mails

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%