Last active
August 2, 2018 19:56
-
-
Save AO8/c288b60c3901aef772e59cbd1e09f462 to your computer and use it in GitHub Desktop.
Continuously email a random Stoic meditation from Marcus Aurelius: a simple exercise in file reading, timedeltas, and sending gmail with Python.
This file contains hidden or 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
| # Builds on the random daily Stoic meditation gist at: | |
| # https://gist.github.com/AO8/a33b0e70a1885679279f0533d4f5d8dc | |
| # Enable less secure apps in gmail before running | |
| import random | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| from datetime import datetime, timedelta | |
| from time import sleep | |
| def send_gmail(from_addr, password, to_addr, subject, body): | |
| # prepare email | |
| msg = MIMEText(body) | |
| msg["Subject"] = subject | |
| msg["From"] = from_addr | |
| msg["To"] = to_addr | |
| # connect, send, quit | |
| s = smtplib.SMTP("smtp.gmail.com: 587") | |
| s.ehlo() | |
| s.starttls() | |
| s.login(from_addr, password) | |
| s.send_message(msg) | |
| s.quit() | |
| start = datetime.now() | |
| stop = start + timedelta(#define when program should stop using days=, hours=, or seconds=) | |
| while datetime.now() < stop: | |
| random.seed(datetime.now()) | |
| with open("meditations.txt", encoding="utf8") as f: | |
| contents = f.readlines() | |
| body = random.choice(contents) + "\n" + "~ Marcus" | |
| subject = body[0:30] + "..." | |
| with open("subscribers.txt", encoding="utf8") as f: | |
| subscribers = f.readlines() | |
| for subscriber in subscribers: | |
| send_gmail("#your gmail address", | |
| "#your password", | |
| subscriber, | |
| subject, | |
| body) | |
| sleep(#seconds until loop starts over) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment