Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active July 31, 2018 21:21
Show Gist options
  • Select an option

  • Save AO8/fa3280d201a111f2a27757f9e1992aea to your computer and use it in GitHub Desktop.

Select an option

Save AO8/fa3280d201a111f2a27757f9e1992aea to your computer and use it in GitHub Desktop.
Randomly select and email a daily Stoic meditation from Marcus Aurelius: a simple exercise in file reading and sending gmail with Python.
# Builds on the random daily Stoic meditation gist at:
# https://gist.github.com/AO8/a33b0e70a1885679279f0533d4f5d8dc
# Enable less secure apps in gmail before attempting to send
import random
import smtplib
from email.mime.text import MIMEText
from datetime import datetime
random.seed(datetime.now())
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()
# Separate txt file containing favorite passages from
# Meditations by Marcus Aurelius
with open("meditations.txt", encoding="utf8") as f:
contents = f.readlines()
body = random.choice(contents) + "\n" + "~ Marcus A."
subject = body[0:30] + "..."
# Read email addresses in separate txt file
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment