Created
June 11, 2017 19:23
-
-
Save poanchen/95851e00011dc85637ff9694fac1becc to your computer and use it in GitHub Desktop.
A python program which scans a folder called Monday, in that folder will be text files and each of them will have an email address on the first line. The program will simply send an email using smtp. This snippet of code is used to answer this question on Quora. https://www.quora.com/How-do-I-write-a-python-program-that-scans-a-folder-looks-at-e…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to run the program | |
# python run.py | |
# folders structure | |
# |->run.py | |
# |->Monday/ | |
# |->Monday/a.txt | |
# |->Monday/b.txt | |
# |->Monday/c.txt | |
# | |
# Example of a.txt file: | |
# cat a.txt | |
# [email protected] | |
# | |
# Example of the cron job: | |
# Runs the `run.py` on every Monday at 8:00AM | |
# 0 8 * * 1 python /path/to/run.py >> /path/to/run.log | |
# | |
# https://crontab.guru/#0_8_*_*_1 | |
import glob, os, smtplib | |
diretory = "./Monday" | |
# head over to the diretory where it holds | |
# all the txt files. | |
os.chdir(diretory) | |
# get all the txt file name and | |
# store them into a list | |
files = [file for file in glob.glob("*.txt")] | |
if len(files) == 0: | |
print """ | |
[WARNING] No txt files exist in %s | |
""".strip() % (diretory) | |
quit() | |
# open each files and read the first line only | |
# and store them into a list | |
emails = [] | |
for file in files: | |
try: | |
with open(file, 'r') as f: | |
# assuming the first line exist | |
# and it is an email address | |
emails.append(f.readline()) | |
except IOError: | |
print """ | |
[WARNING] Cannot open file: %s | |
""".strip() % (file) | |
f.close() | |
for email in emails: | |
# you must provide your gamil credential | |
# so that you can send email on your behalf | |
sender_gmail = 'GMAIL_USERNAME' | |
sender_gmail_password = 'GMAIL_PASSWORD' | |
headers = "\r\n".join(["from: " + sender_gmail, | |
"subject: Weekly update...", | |
"to: " + email, | |
"mime-version: 1.0", | |
"content-type: text/html"]) | |
body_of_email = """ | |
Hi there,<br><br> | |
Put anything you want to say in the email. | |
I have left an exercise for you, | |
here is an article teaches how you can send attachment along with the email | |
https://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python | |
This message was intended to sent to %s. | |
""" % (email) | |
email_body = headers + "\r\n\r\n" + body_of_email | |
try: | |
server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465) | |
# in case you need to turn on the debug mode | |
# server_ssl.set_debuglevel(1) | |
server_ssl.login(sender_gmail, sender_gmail_password) | |
server_ssl.sendmail(sender_gmail, email, email_body) | |
server_ssl.quit() | |
print '[INFO] Succesully send the email to ' + email | |
except: | |
print '[WARNING] Failed to send the email to ' + email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment