From 4f4fc2b6094f90b486ed95e4dac6120ad298acd0 Mon Sep 17 00:00:00 2001 From: thisfro Date: Sun, 23 Apr 2023 13:28:51 +0200 Subject: [PATCH 1/6] Add logging and more options --- mail.py | 26 +++++++++++++++++++------- vars.py.example | 5 ++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/mail.py b/mail.py index 052e633..bfbbd45 100644 --- a/mail.py +++ b/mail.py @@ -1,10 +1,9 @@ +import logging import smtplib import time - -from string import Template - from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from string import Template import pandas as pd @@ -17,6 +16,8 @@ def get_contacts(filename): read from a file specified by filename. """ + logging.debug(f'Loading contacts from {filename}...') + return pd.read_csv(filename) @@ -32,6 +33,10 @@ def read_template(filename): def main(): + logging.basicConfig(level=LOGLEVEL, format='%(asctime)s - %(levelname)s - %(message)s') + + logging.info('Starting pymail') + contacts = get_contacts('contacts.csv') message_template = read_template('message.html') @@ -39,10 +44,13 @@ def main(): s = smtplib.SMTP(HOST, PORT) s.starttls() s.login(MY_ADDRESS, PASSWORD) + logging.debug('Mail server is set up.') + + logging.info(f'Starting to send mails to {len(contacts.index)} recipients, this might take a while...') # For each contact, send the email: for i in contacts.index: - print(f"Sending email {i+1}\n") + logging.info(f"Sending email {i+1} of {len(contacts.index)}\n") msg = MIMEMultipart() # create a message # add in the actual person name to the message template @@ -50,7 +58,7 @@ def main(): message = message_template.substitute(contact_dict) # Prints out the message body for our sake - print(f"{message}\n") + logging.debug(f"{message}\n") # setup the parameters of the message msg['From'] = MY_ADDRESS @@ -60,16 +68,20 @@ def main(): # add in the message body msg.attach(MIMEText(message, 'html')) - # send the message via the server set up earlier. - s.send_message(msg) + if not DRY_RUN: + # send the message via the server set up earlier. + s.send_message(msg) del msg + # Wait for DELAY_SEC seconds every WAIT_EVERY-th message if (i + 1) % WAIT_EVERY == 0: time.sleep(DELAY_SEC) # Terminate the SMTP session and close the connection s.quit() + logging.info('Done!') + if __name__ == '__main__': main() diff --git a/vars.py.example b/vars.py.example index bbd94a3..8c1a91e 100644 --- a/vars.py.example +++ b/vars.py.example @@ -7,4 +7,7 @@ HOST = 'mail.domain.tld' PORT = 587 WAIT_EVERY = 5 -DELAY_SEC = 0 \ No newline at end of file +DELAY_SEC = 0 + +LOGLEVEL = 'INFO' +DRY_RUN = True \ No newline at end of file From 78e74d5ae99c4487ea66d3ee4f2f7b6d12b4e096 Mon Sep 17 00:00:00 2001 From: thisfro Date: Sun, 23 Apr 2023 13:28:58 +0200 Subject: [PATCH 2/6] Fix name --- vars.py.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars.py.example b/vars.py.example index 8c1a91e..f1c6554 100644 --- a/vars.py.example +++ b/vars.py.example @@ -1,5 +1,5 @@ SUBJECT = '' -EMAIL_COLUMN = '' +MAIL_COLUMN = '' MY_ADDRESS = 'example@domain.tld' PASSWORD = 'p4ssw0rd' From b05a1079d6a87b4c5106315e14277236472c6ddf Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 29 Sep 2023 20:20:35 +0200 Subject: [PATCH 3/6] Add venv to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 11358ad..833a6b9 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vars.py message.html .DS_Store *.pyc +venv \ No newline at end of file From 754184628eb12ae5ad226df434552c2192d69e8f Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 29 Sep 2023 20:20:45 +0200 Subject: [PATCH 4/6] Add requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1411a4a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pandas \ No newline at end of file From 746e44942f42b3409bc31b981442393b08c21363 Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 29 Sep 2023 20:21:09 +0200 Subject: [PATCH 5/6] Update readme --- README.md | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f101fb3..d5d4039 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,27 @@ Easily send mails via SMTP and python -## Setup -1. Copy `vars.py.example` to `vars.py` -2. Setup `vars.py` with your credentials and config -3. Adapt `message.htmt` and save your contact list to `contacts.csv` +## Installation +To install the requirements, run the following command +``` +python -m pip install -r requirements.txt +``` ## Usage -```python mail.py``` + +### Setup +1. Copy `vars.py.example` to `vars.py` +2. Setup `vars.py` with your credentials and config +3. Adapt `message.html` and save your contact list to `contacts.csv` + +### Send mail +Just run +```sh +python mail.py +``` ## Templating -You can use the keys from your `csv` directly, prepending a `$` sign. For example: +You can use the keys from your `csv` directly, prepending a `$` sign (case sensitive!). For example: ``` Hello $name, your value is $value! ``` @@ -23,4 +34,14 @@ Hello h4xx0r, your value is 1337! With a `contacts.csv` looking like: | name | value | |--------|-------| -| h4xx0r | 1337 | \ No newline at end of file +| h4xx0r | 1337 | + +## Options +### Sending behavior +Some mail servers will block the requests, if too many are sent too fast. You can use the variables to add delays: +- `WAIT_EVERY` the amount of mails sent at once +- `DELAY_SEC` the amount of seconds to wait between sending the next batch + +### Other options +- `LOGLEVEL` the logging level, set to `'DEBUG'` for most output +- `DRY_RUN` if set to `True`, the mails wont get sent, but the output is generated as if they would be sent. Good for testing and best to use with `LOGLEVEL = 'DEBUG'` \ No newline at end of file From d268720796c5710950d738bf5bc1b31bb4c9128e Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 29 Sep 2023 20:21:35 +0200 Subject: [PATCH 6/6] Add template message --- message.html | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 message.html diff --git a/message.html b/message.html new file mode 100644 index 0000000..a8cca27 --- /dev/null +++ b/message.html @@ -0,0 +1,5 @@ + + +

Hello $name, your value is $value!

+ + \ No newline at end of file