Compare commits
3 commits
d74234a8e7
...
02b76b5755
Author | SHA1 | Date | |
---|---|---|---|
02b76b5755 | |||
0bb4a1d06c | |||
f6abda8772 |
4 changed files with 21 additions and 6 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
vars.py
|
||||||
|
*.csv
|
||||||
|
message.html
|
||||||
|
.DS_Store
|
||||||
|
*.pyc
|
|
@ -4,8 +4,8 @@ Easily send mails via SMTP and python
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
1. Copy `vars.py.example` to `vars.py`
|
1. Copy `vars.py.example` to `vars.py`
|
||||||
2. Setup `vars.py` with your credentials
|
2. Setup `vars.py` with your credentials and config
|
||||||
3. Adapt `message.txt` and import `contacts.csv`
|
3. Adapt `message.htmt` and save your contact list to `contacts.csv`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
```python mail.py```
|
```python mail.py```
|
||||||
|
|
11
mail.py
11
mail.py
|
@ -1,4 +1,5 @@
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import time
|
||||||
|
|
||||||
from string import Template
|
from string import Template
|
||||||
|
|
||||||
|
@ -32,7 +33,7 @@ def read_template(filename):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
contacts = get_contacts('contacts.csv')
|
contacts = get_contacts('contacts.csv')
|
||||||
message_template = read_template('message.txt')
|
message_template = read_template('message.html')
|
||||||
|
|
||||||
# set up the SMTP server
|
# set up the SMTP server
|
||||||
s = smtplib.SMTP(HOST, PORT)
|
s = smtplib.SMTP(HOST, PORT)
|
||||||
|
@ -41,6 +42,7 @@ def main():
|
||||||
|
|
||||||
# For each contact, send the email:
|
# For each contact, send the email:
|
||||||
for i in contacts.index:
|
for i in contacts.index:
|
||||||
|
print(f"Sending email {i+1}\n")
|
||||||
msg = MIMEMultipart() # create a message
|
msg = MIMEMultipart() # create a message
|
||||||
|
|
||||||
# add in the actual person name to the message template
|
# add in the actual person name to the message template
|
||||||
|
@ -48,11 +50,11 @@ def main():
|
||||||
message = message_template.substitute(contact_dict)
|
message = message_template.substitute(contact_dict)
|
||||||
|
|
||||||
# Prints out the message body for our sake
|
# Prints out the message body for our sake
|
||||||
print(message)
|
print(f"{message}\n")
|
||||||
|
|
||||||
# setup the parameters of the message
|
# setup the parameters of the message
|
||||||
msg['From'] = MY_ADDRESS
|
msg['From'] = MY_ADDRESS
|
||||||
msg['To'] = contacts["email"][i]
|
msg['To'] = contacts[MAIL_COLUMN][i]
|
||||||
msg['Subject'] = SUBJECT
|
msg['Subject'] = SUBJECT
|
||||||
|
|
||||||
# add in the message body
|
# add in the message body
|
||||||
|
@ -62,6 +64,9 @@ def main():
|
||||||
s.send_message(msg)
|
s.send_message(msg)
|
||||||
del msg
|
del msg
|
||||||
|
|
||||||
|
if (i + 1) % WAIT_EVERY == 0:
|
||||||
|
time.sleep(DELAY_SEC)
|
||||||
|
|
||||||
# Terminate the SMTP session and close the connection
|
# Terminate the SMTP session and close the connection
|
||||||
s.quit()
|
s.quit()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
|
SUBJECT = ''
|
||||||
|
EMAIL_COLUMN = ''
|
||||||
|
|
||||||
MY_ADDRESS = 'example@domain.tld'
|
MY_ADDRESS = 'example@domain.tld'
|
||||||
PASSWORD = 'p4ssw0rd'
|
PASSWORD = 'p4ssw0rd'
|
||||||
HOST = 'mail.domain.tld'
|
HOST = 'mail.domain.tld'
|
||||||
PORT = 587
|
PORT = 587
|
||||||
SUBJECT = ''
|
|
||||||
|
WAIT_EVERY = 5
|
||||||
|
DELAY_SEC = 0
|
Loading…
Reference in a new issue