[TriEmbed] programmatic email with a Raspberry Pi or the like

Adam Haile email at adamhaile.net
Tue Nov 10 11:51:10 CST 2015


I knew I had it working with gmail...
Try this out:
_username = "user"
_password = "password"
_fromname = "Your Name"



def send_msg(_to_addr, _subject, _body):
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(_username, _password)

    print "To: %s" % _to_addr
    print "Subject: %s" %_subject

    msg = MIMEText(_body)
    msg['Subject'] = _subject
    msg['From'] = email.utils.formataddr((_fromname, _username))
    msg['To'] = _to_addr

    server.sendmail(_username, [_to_addr], msg.as_string())

On Tue, Nov 10, 2015 at 12:40 PM, Pete Soper via TriEmbed <
triembed at triembed.org> wrote:

> 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:-------")
>
>
> _______________________________________________
> Triangle, NC Embedded Computing mailing list
> TriEmbed at triembed.org
> http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
> TriEmbed web site: http://TriEmbed.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20151110/b60b7840/attachment.htm>


More information about the TriEmbed mailing list