Skip to content

Instantly share code, notes, and snippets.

@WardsParadox
Created September 29, 2015 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WardsParadox/a3935a44a3b749761a1e to your computer and use it in GitHub Desktop.
Save WardsParadox/a3935a44a3b749761a1e to your computer and use it in GitHub Desktop.
SMTP Mailer in python for Cacher. I couldn't get the Mail to send via the OS X Server interface. Found mass majority on StackOverflow
#!/usr/bin/env python
# Settings
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 465
SMTP_USERNAME = 'EMAILHERE'
SMTP_PASSWORD = 'PASSHERE'
SMTP_FROM = 'SAMEASUSERNAME'
SMTP_TO = 'TOEMAIL'
# Now construct the message
import smtplib, email, subprocess, time
from email import encoders
MESSAGE = subprocess.compname = subprocess.Popen("/usr/local/etc/cacher", shell=False, stdout=subprocess.PIPE).communicate()[0]
date = (time.strftime("%a on %m/%d/%Y"))
SMTP_SUBJECT = "Caching Server Report for %s" % date
msg = email.MIMEMultipart.MIMEMultipart()
body = email.MIMEText.MIMEText(MESSAGE)
# attachment = email.MIMEBase.MIMEBase('text', 'plain')
# attachment.set_payload(open(TEXT_FILENAME).read())
# attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(TEXT_FILENAME))
# encoders.encode_base64(attachment)
msg.attach(body)
# msg.attach(attachment)
msg.add_header('From', SMTP_FROM)
msg.add_header('To', SMTP_TO)
msg.add_header('Subject', SMTP_SUBJECT)
# Now send the message
mailer = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
# EDIT: mailer is already connected
# mailer.connect()
mailer.login(SMTP_USERNAME, SMTP_PASSWORD)
mailer.sendmail(SMTP_FROM, [SMTP_TO], msg.as_string())
mailer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment