This section provides various examples for various needs.

Email Campaign

In case you have a list of clients or customers you wish to send personalized emails, you may benefit from templating. It may help to make the templates to an HTML file, polish them and after then send:

from redmail import EmailSender

email = EmailSender(...)
email.receivers = ['']

Then make a HTML file, for example path/to/campaigns/summer_sale.html:

<img src="{{ company_logo }}" width=200 height=100>
<h1>Thank you, {{ customer }}, for being awesome!</h1>
    We are pleased to inform you that we have a lot of products
    in huge discounts.
{% for product, discount, in discounts.items() %}
    <li>{{ product }}: {{ '{:.0f} %'.format(discount * 100) }}</li>
{% endfor %}
<p>Kind regards, We Ltd.</p>

Finally send the emails:

discounts = {'shoes': 0.2, 'shirts': 0.4}
customers = ['', '', ...]
for customer in customers:
        subject="Summer Sale!",
            "customer": customer,
            "discounts": discounts
            "company_logo": "path/to/logo.png"

Error Alerts

If you are building long running program (ie. web app) you can make a templated error alerts that include the full traceback:

from redmail import EmailSender

error_email = EmailSender(...)
error_email.sender = ''
error_email.receivers = ['']
error_email.html = """
    <h2>An error encountered</h2>
    {{ error }}

    raise RuntimeError("Oops")
    # Send an email including the traceback
    error_email.send(subject="Fail: doing stuff failed")


The error formatting object identifies which body it is being attached to. If you wish to use text body, error will show up similarly as Python errors you see on terminals. See more from redmail.models.Error

Stats Reports

As demonstrated here, embedding Matplotlib figures to the HTML bodies is trivial. Therefore you can easily create diagnostic reports or automatic analyses. Just create the plots and let Red Mail send them to you:

from redmail import EmailSender

stats_report = EmailSender(...)
stats_report.sender = ''
stats_report.receivers = ['']

# Create a plot
import matplotlib.pyplot as plt
fig_performance = plt.Figure()

# Create summary table
import pandas as pd
df = pd.DataFrame(...)
df_summary = df.describe()

# Send the report
    subject="System Diagnostics",
        <h1>System Diagnostics ({{ now }})</h1>
        {{ perf_plot }}
        <h2>Summary Statistics</h2>
        {{ tbl_summary }}
        <p>System running on {{ node }}</p>
        "perf_plot": fig_performance,
        "tbl_summary": df_summary

Distribution Lists

There might be a situation in which you would like to specify some sets of pre-defined distribution lists for which you will send emails to depending on situation. To accomplish this, you can create subclass the EmailSender and create cystin distribution list logic:

from redmail import EmailSender

class DistributionSender(EmailSender):
    "Send email using pre-defined distribution lists"

    def __init__(self, *args, distributions:dict, **kwargs):
        super().__init__(*args, **kwargs)
        self.distributions = distributions

    def get_receivers(self, receiver_list):
        if receiver_list:
            return self.distributions[receiver_list]

    def get_cc(self, receiver_list):
        if receiver_list:
            return self.distributions[receiver_list]

    def get_bcc(self, receiver_list):
        if receiver_list:
            return self.distributions[receiver_list]

Then to use it:

email = DistributionSender(
    host="localhost", port=0,
        "managers": ["", ""],
        "developers": ["", ""]

    subject="Important news",

You can also accomplish this without subclassing to limited extent:

managers = EmailSender(host="localhost", port=0)
managers.receivers = ["", ""]

developers = EmailSender(host="localhost", port=0)
developers.receivers = ["", ""]

# Send an email to the developers
    subject="Important news"