pyautomail
pyautomail.cli
- delete_process(pid: int | None = <typer.models.ArgumentInfo object>, dry_run: bool = <typer.models.OptionInfo object>) None [source]
Delete a process.
- delete_record(rid: int | None = <typer.models.ArgumentInfo object>, dry_run: bool = <typer.models.OptionInfo object>) None [source]
Delete a record by ID.
- init(db_path: str = <typer.models.OptionInfo object>, smtp_server: str = <typer.models.OptionInfo object>, smtp_port: int = <typer.models.OptionInfo object>, email: str = <typer.models.OptionInfo object>, password: str = <typer.models.OptionInfo object>, is_test: bool = <typer.models.OptionInfo object>) None [source]
Initialize the mail database.
- list_records(process_id: int | None = <typer.models.ArgumentInfo object>) None [source]
List all records of one process.
- register(email: str = <typer.models.OptionInfo object>, contact_list: ~pathlib.Path = <typer.models.ArgumentInfo object>, title: str = <typer.models.OptionInfo object>, custom_pdf: bool = <typer.models.OptionInfo object>, attachment: ~pathlib.Path = <typer.models.OptionInfo object>, custom_pdf_dir: ~pathlib.Path = <typer.models.OptionInfo object>, subject: str = <typer.models.OptionInfo object>, template: ~pathlib.Path | None = <typer.models.OptionInfo object>) None [source]
Register your email account.
- run_process(pid, is_resume=True, wait_time=0.05)[source]
This function will run a process with a specific id
- Parameters:
pid (int) – the process id
is_resume (bool) – if True, the program will resume the process if it is paused, otherwise it prints a warning message and return without doing anything
wait_time (int) – the time to wait between sending emails
pyautomail.emailsender
- class EmailSender(user, password, host='smtp.gmail.com', port=465, is_test=False, log_file=None, log_level=10)[source]
Bases:
object
This class is for sending emails to multiple users.
This class uses a template engine to render the email body. Currently, it supports two template types: ‘.txt’ and ‘.html’. You can use the template type that suits your needs. The template engine that is used in this class is Jinja2. You can find more information about Jinja2 in the following link: https://jinja.palletsprojects.com/en/2.11.x/
Examples
>>> from pyautomail import EmailSender >>> sender = EmailSender(cfg="config.cfg") >>> sender.set_template('body.txt') >>> data = {'name': 'Jon', 'age': 30} >>> sender.send('contact1@gmail.com', 'sub1', data)
a simple example of a template file:
body.txt:
Hello {{name}}, your age is {{age}}.
a simple example of a config.cfg file:
[smtp] host = smtp.gmail.com port = 465 is_test = True [account] user = "" password = ""
Or you can use the class without a config file:
from automail import EmailSender sender = EmailSender(user="your-email-address", password="your-password") sender.set_template('body.txt') data = {'name': 'Jon', 'age': 30} sender.send('contact1@gmail.com', 'sub1', data)
- __init__(user, password, host='smtp.gmail.com', port=465, is_test=False, log_file=None, log_level=10)[source]
- Parameters:
user (str) – The email address of the sender.
password (str) – The password of the sender.
host (str) – The host of the SMTP server.
port (int) – The port of the SMTP server.
is_test (bool) – If this flag is set, no email will be sent. This flag is useful for testing purposes.
log_file (str) – The path to the log file.
log_level (int) –
The level of the logger. The default value is 100. You can use the following values:
10: DEBUG
20: INFO
30: WARNING
40: ERROR
50: CRITICAL
- classmethod from_config(cfg=None)[source]
This method is used to initialize this class from a config file.
- render_template(data)[source]
This method is used by send method.
- Parameters:
data – dictionary of data to be replaced in the template.
- Returns:
body: the string of the rendered template.
- send(receiver_email_address: str, subject: str, data: dict, attachment_path=None, from_address=None) dict [source]
- Parameters:
receiver_email_address – Email address of the receiver.
subject – Subject of the email.
data – dictionary of data to be replaced in the template.
attachment_path – Path to attachment file.
from_address – Email address of the sender.
- Returns:
pyautomail.manager
This module is responsible for managing the pyautomail pkg. It has below commands:
- register: register a new contact list and a new process this command will get below arguments:
username: the email address of the sender
contacts: the path to the csv file which contains the contacts
name(optional): an arbitrary name for the process
subject(optional): the subject of the email
template(optional): the path to the template file
cpdf(optional): this is a flag that if it is set, the pdf file will be customized for each contact based on the path to the pdf file which is in the contacts csv file in the column ‘cpdf’
attachment(optional): the path to the attachment file if you want to attach a same file to all emails
pdf_dir(optional): the path to the directory which contains the pdf files if you want to use cpdf flag
start: this command will start a process and get the process id as argument you can find the process id in the list of processes by: pyautomail list command
list: list processes
stop: this command will stop one specific process by process id
resume: this command will resume one specific process by process id
- list_processes(pid=None, **kwargs)[source]
This function will print the list of all processes or a specific process with a specific id
- Parameters:
pid (int) – the process id if you want to see the information of a specific process or None if you want to see the information of all processes
- registration(username, contacts, name='', cpdf=False, attachment='', pdf_dir='', **args)[source]
This function will register a new process and a new contact list
- Parameters:
username (str) – the email address of the sender
contacts (str) – the path to the csv file which contains the contacts
name (str) – an arbitrary name for the process
cpdf (bool) – this is a flag that if it is set, the pdf file will be customized for each contact based on the path to the pdf file which is in the contacts csv file in the column ‘cpdf’
attachment (str) – the path to the attachment file if you want to attach a same file to all emails
pdf_dir (str) – the path to the directory which contains the pdf files if you want to use cpdf flag
args (dict) – the other arguments which are passed to the function
- Return type:
None
Notes
This function will register a new process plus records for each contact in the contacts csv file.
pyautomail.utils
- class AutomailLogHandler(user, password, capacity=1, to=None, host='smtp.gmail.com', port=465, formatter='%(asctime)s %(levelname)-5s %(message)s', sub='no-reply-logger', plain_temp='{{ msg }}', template_path=None)[source]
Bases:
BufferingHandler
- create_config_file(smtp_server, smtp_port, sender_email='<enter-your-email>', password='<enter-your-password>', is_test=False)[source]
This function will create a config file for the email sender.
- Parameters:
smtp_server (str) – The SMTP server address.
smtp_port (int) – The SMTP server port.
sender_email (str) – The email address of the sender.
password (str) – The password of the sender.
is_test (bool) – If True, the email will not be sent.
Notes
This function will create a config file with the following format:
[SMTP] server = smtp.gmail.com port = 465 [SENDER] email = example@gmail.com password = password
- get_config_dict()[source]
This function will get the email sender config.
- Returns:
config – The config object.
- Return type:
configparser.ConfigParser
Notes
This function will read the config file with the following format:
[SMTP] server = smtp.gmail.com port = 465 [SENDER] email = password =
- init_logger(name='', filename=None, level=50)[source]
This function will initialize a logger object
- Parameters:
name (str) – The name of the logger object. If not specified, the name will be the root logger.
filename (str) – The path where the log file is created. If it isn’t set, no log file is created.
level (int) – the level of logger
- Returns:
logger_obj – The logger object.
- Return type:
logging.Logger
Notes
This function will initialize a logger object with the following format:
[%(asctime)s - %(levelname)s (%(name)s) ] : %(message)s
Example
The logger object will log to both stdout and a file. After colling this function you can use logger_obj to log.
>>> logger_obj = init_logger('EmailSender', level=logging.DEBUG) >>> logger_obj.debug('debug message') ... - DEBUG (EmailSender) ] : debug message >>> logger_obj.info('info message') ... - INFO (EmailSender) ] : info message >>> logger_obj.warning('warning message') ... - WARNING (EmailSender) ] : warning message >>> logger_obj.error('error message') ... - ERROR (EmailSender) ] : error message >>> logger_obj.critical('critical message') ... - CRITICAL (EmailSender) ] : critical message
- read_config_file()[source]
This function will read the config file.
- Returns:
config – The config object.
- Return type:
configparser.ConfigParser
Notes
This function will read the config file with the following format:
[SMTP] server = smtp.gmail.com port = 465 [SENDER] email = exxample@gmail.com password = password