Last active
February 1, 2018 04:42
-
-
Save adikari/070b79a649d1c22e9eb5fcc05b77375e to your computer and use it in GitHub Desktop.
script to book parking
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
{ | |
"from": "", | |
"to": "", | |
"subject" : "parking request for tomorrow" | |
} |
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
Hello Receiver, | |
Can you please book a parking spot for me for tomorrow? | |
Thanks, | |
Sender |
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
# send email every weekday at 2:15 pm with email content from content.txt | |
15 14 * * 1-4 python apsolute_path_to_script | |
# Send email on friday at 2:15 pm with a email content from template.txt | |
15 14 * * 5 python apsolute_path_to_script template.txt |
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
#!/usr/bin/env python | |
import smtplib, json, os, logging, sys | |
from email.mime.text import MIMEText | |
''' | |
Usage: ./parking Sends email using default template | |
./parking template.txt Sends email using custom template | |
''' | |
config = 'config.json' | |
content = 'content.txt' if len(sys.argv) < 2 else sys.argv[1] | |
log = 'logs' | |
current_dir = os.path.dirname(os.path.realpath(__file__)) + '/' | |
logger = logging.getLogger('parking-emailer') | |
handler = logging.FileHandler(current_dir + log) | |
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
logger.setLevel(logging.INFO) | |
try: | |
with open(current_dir + config) as data_file: | |
data = json.load(data_file) | |
me = data['from'] | |
you = data['to'] | |
subject = data['subject'] | |
fp = open(current_dir + content, 'rb') | |
msg = MIMEText(fp.read()) | |
fp.close() | |
msg['Subject'] = subject | |
msg['From'] = me | |
msg['To'] = you | |
s = smtplib.SMTP('localhost') | |
try: | |
s.sendmail(me, [you], msg.as_string()) | |
logger.info("Parking request made successfully.") | |
except smtplib.SMTPException as e: | |
logger.error("Error sending mail: {0}".format(str(e))) | |
finally: | |
s.quit() | |
except IOError as e: | |
logger.error("I/O error ({0}): {1} - {2}".format(e.errno, e.strerror, e.filename)) | |
except Exception as e: | |
logger.error("Unexpected error: {0}".format(str(e))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment