Improve templating and use pandas for contacts
This commit is contained in:
parent
6372173fa3
commit
d74234a8e7
2 changed files with 31 additions and 34 deletions
10
README.md
10
README.md
|
@ -9,3 +9,13 @@ Easily send mails via SMTP and python
|
|||
|
||||
## Usage
|
||||
```python mail.py```
|
||||
|
||||
## Templating
|
||||
You can use the keys from your `csv` directly, prepending a `$` sign. For example:
|
||||
```
|
||||
Hello $name, your value is $value!
|
||||
```
|
||||
would look like the following:
|
||||
```
|
||||
Hello h4xx0r, your value is 1337!
|
||||
```
|
33
mail.py
33
mail.py
|
@ -5,34 +5,19 @@ from string import Template
|
|||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
import csv
|
||||
import pandas as pd
|
||||
|
||||
from vars import *
|
||||
|
||||
|
||||
def get_contacts(filename):
|
||||
"""
|
||||
Return the lists containing the infos
|
||||
read from a file specified by filename.
|
||||
"""
|
||||
|
||||
snames = []
|
||||
fnames =[]
|
||||
emails = []
|
||||
pwd = []
|
||||
user = []
|
||||
with open(filename, mode='r', encoding='utf-8') as contacts_file:
|
||||
for a_contact in contacts_file:
|
||||
snames.append(a_contact.split()[0])
|
||||
fnames.append(a_contact.split()[1])
|
||||
emails.append(a_contact.split()[2])
|
||||
pwd.append(a_contact.split()[3])
|
||||
user.append(a_contact.split()[4])
|
||||
return snames, fnames, emails, pwd, user
|
||||
return pd.read_csv(filename)
|
||||
|
||||
def read_csv(filename):
|
||||
with open(filename, newline='') as csvfile:
|
||||
data = list(csv.reader(csvfile))
|
||||
return data
|
||||
|
||||
def read_template(filename):
|
||||
"""
|
||||
|
@ -44,9 +29,9 @@ def read_template(filename):
|
|||
template_file_content = template_file.read()
|
||||
return Template(template_file_content)
|
||||
|
||||
|
||||
def main():
|
||||
# names, emails, reda, ws = get_contacts('mycontacts.txt') # read contacts
|
||||
contacts = read_csv('contacts.csv')
|
||||
contacts = get_contacts('contacts.csv')
|
||||
message_template = read_template('message.txt')
|
||||
|
||||
# set up the SMTP server
|
||||
|
@ -55,18 +40,19 @@ def main():
|
|||
s.login(MY_ADDRESS, PASSWORD)
|
||||
|
||||
# For each contact, send the email:
|
||||
for i in range(len(contacts)):
|
||||
for i in contacts.index:
|
||||
msg = MIMEMultipart() # create a message
|
||||
|
||||
# add in the actual person name to the message template
|
||||
message = message_template.substitute(SNAME=str(contacts[i][0]), FNAME=contacts[i][1], PWD=contacts[i][3], USER =contacts[i][4])
|
||||
contact_dict = {col: contacts[col][i] for col in contacts.columns}
|
||||
message = message_template.substitute(contact_dict)
|
||||
|
||||
# Prints out the message body for our sake
|
||||
print(message)
|
||||
|
||||
# setup the parameters of the message
|
||||
msg['From'] = MY_ADDRESS
|
||||
msg['To']=contacts[i][2]
|
||||
msg['To'] = contacts["email"][i]
|
||||
msg['Subject'] = SUBJECT
|
||||
|
||||
# add in the message body
|
||||
|
@ -79,5 +65,6 @@ def main():
|
|||
# Terminate the SMTP session and close the connection
|
||||
s.quit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue