Tutorial

Note

To install pyautomail please see installation

Config file

Config files have several sections with different attributes, which are described below.

  • [smtp]: SMTP server settings
    • host: SMTP server host

    • port: SMTP server port

    • is_test: If True, emails will not be sent

  • [account]: Email settings
    • user: Email user

    • password: Email password

  • [log]: Logging settings
    • level: Logging level 10: DEBUG, 20: INFO, 30: WARNING, 40: ERROR, 50: CRITICAL

    • file: Log file path

[smtp]
host = smtp.gmail.com
port = 465
is_test = True

[account]
user = example@gmail.com
password = 12345678

[log]
file-path = ./log.log
level = 10

Templates

Templates are used to generate the email body. There is three types of templates:

  • html: HTML template

  • text: Text template

  • string: String template(just used in python code)

the variables in the template are {{ <variable-name> }} and they are the column name of contact list or the key values in the input data dictionary of EmailSender.send method.

Two examples of template files are coming below:

template.html:

<html>
    <head></head>
    <body>
        <p>Hi {{ name }},</p>
        <p>This is a test email.</p>
        <p>Thanks,</p>
        <p>{{ sender }}</p>
    </body>
</html>

template.txt:

Hi {{ name }},

This is a test email.

Thanks,

{{ sender }}

Python usage

Below is a Python script that demonstrates how to send a single email:

from pyautomail import EmailSender

# Creating a sender object
sender = EmailSender(cfg="./assets/config.cfg")
# Set the template
sender.set_template('./templates/body.txt')
# Custom data
data = {'name': 'Jon', 'age': 30}
# Send the email to 'contact1@gmail.com' with Subject 'sub1' and custom date
sender.send('contact1@gmail.com', 'sub1', data)
# Repeat the above steps but using a '.html' template
data = {'name': 'Mike', 'age': 30}
sender.set_template('./templates/html.html')
sender.send('contact2@gmail.com', 'sub2', data)
# plain_temp
data = {'name': 'Mike', 'age': 30}
sender.set_template(plain_temp="{{ name }}-{{ age }}")
sender.send('contact2@gmail.com', 'sub2', data)

CLI Usage

Note

After installation, you can use the command line tool pyautomail to send emails.