Skip to content

Commit

Permalink
Adding 'auto_mailer.py' script to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ndarin committed Jul 12, 2021
1 parent de527a4 commit 79c87be
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
44 changes: 44 additions & 0 deletions auto_mailer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

from email.message import EmailMessage
import smtplib
import getpass
import os
import mimetypes
#Defining the email labels
message = EmailMessage()
sender = '[email protected]'
recipient = '[email protected]'
body = """Hi.
I'm learning how to send an email using Python.
How cool is that?"""
#Setting the email keys and labels values
message['From'] = sender
message['To'] = recipient
message['Subject'] = "Greetings from {} to {}".format(sender, recipient)
message.set_content(body)

#Defining attachment variables
attachment_path = 'C:/Users/tawan/images/africa.jpg'
attachment_filename = os.path.basename(attachment_path)
mime_type, _ = mimetypes.guess_type(attachment_path)
mime_main, mime_subtype = mime_type.split('/', 1)

#Adding the email attachment
with open(attachment_path, 'rb') as ap:
message.add_attachment(ap.read(),
maintype=mime_main,
subtype=mime_subtype,
filename=os.path.basename(attachment_path))

#Seting up mail server connection
mail_server = smtplib.SMTP_SSL('smtp.gmail.com')
mail_server.set_debuglevel(1)
mail_pass = getpass.getpass('Password? ')
mail_server.login(sender, mail_pass)

#Sending the email
mail_server.send_message(message)
#Close the connection
mail_server.quit()
28 changes: 18 additions & 10 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,45 @@
import requests

def create_dict(path):
# Get a list of files in the directory and add the directory path
file_list = os.listdir(path)
dirs = [path + x for x in file_list]

# Create an empty list to store the dictionaries
dict_list = []
keys = 'title', 'name', 'date', 'feedback'

for file in dirs:
# Open each file, read contents, store in a dictionary
# Dictionary keys correspond to data labels in the files
dic = {}
with open(file) as f:
count = 0
for line in f:
line = line.strip()
if count == 0:
dic[keys[count]] = line
dic['title'] = line
elif count == 1:
dic[keys[count]] = line
dic['name'] = line
elif count == 2:
dic[keys[count]] = line
dic['date'] = line
else:
dic[keys[count]] = line
dic['feedback'] = line
count += 1
dict_list.append(dic)
return(dict_list)

def api_call(url, dictionary):
response = requests.post(url, json=dictionary)
if response.status_code != 201:
# Function takes web service url and list of dictionaries
# Pass one dictionary at a time to avoid Error 415.
# Web service expects a dictionary not a list.
for dict in dictionary:
response = requests.post(url, data=dict)
# Print alert if something goes wrong.
if response.status_code != 201:
raise Exception("POST failed with status code {}".format(response.status_code))

def main():
dictionary = create_dict('C:/Users/tawan/data/feedback/')
# Run the functions. 'create_dict' takes directory containing text files
dictionary = create_dict('/data/feedback/')
# 'api_call' takes web service url as a command line argument
api_call(sys.argv[1], dictionary)

if __name__ == "__main__":
Expand Down

0 comments on commit 79c87be

Please sign in to comment.