[TriEmbed] programmatic email with a Raspberry Pi or the like
Pete Soper
pete at soper.us
Tue Nov 10 11:40:01 CST 2015
Following up on last night's conversation I spent another couple hours
horsing around with getting a Python script to send email and I got
something to work with my personal domain server. Implicit at the site
below that the original code came from this "used to work" with Gmail
but Google tightened things up a few years ago. I haven't found more
recent SSL-based incantations on the 'Net to try this with an account
where leakage/loss of the login details would only require destroying
the account. But maybe one of you know about such things to demonstrate
how to do this with Gmail?
Anyway, here's a script that works for me so the next time my water
heater is failing I can hack a leak detector from Lowes into a locked
down single board system to tell me it would be a good idea to come home
and turn the well pump off.
-Pete
#!/usr/bin/python
# Hack of code I found at https://gist.github.com/dbieber/5146518
# pete at soper.us, November, 2015
import getpass, smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
smtp_server = 'smtp.someserver.com'
smtp_user = 'user at somedomain.com'
smtp_password = 'accountpassword'
smtp_port = 587
def mail(to, subject, text, attach=None):
msg = MIMEMultipart()
msg['From'] = smtp_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
if attach:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment;
filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP(smtp_server, smtp_port)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(smtp_user, smtp_password)
mailServer.sendmail(smtp_user, to, msg.as_string())
mailServer.close()
mail("somebody at someplace.com", "FYI", "Here is something you really need
to know:-------")
More information about the TriEmbed
mailing list