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

Pete Soper pete at soper.us
Tue Nov 10 12:08:48 CST 2015


Here's what I get when I try that with my Gmail account (Ubuntu 13.10, 
Python 2.7.5+):

psoper at len:~/p$ ./foo.py
Traceback (most recent call last):
   File "./foo.py", line 29, in <module>
     send_msg('fredfarkel at bitser.net', 'FYI', 'Here is a message')
   File "./foo.py", line 15, in send_msg
     server = smtplib.SMTP('smtp.gmail.com:587')
   File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
     (code, msg) = self.connect(host, port)
   File "/usr/lib/python2.7/smtplib.py", line 311, in connect
     self.sock = self._get_socket(host, port, self.timeout)
   File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
     return socket.create_connection((host, port), timeout)
   File "/usr/lib/python2.7/socket.py", line 553, in create_connection
     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

When I tried the original code off the 'Net Google sent me a msg as if 
paratroopers in blue helmets were sliding down ropes onto the roof of my 
house.

On 11/10/2015 12:51 PM, Adam Haile wrote:
> 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 
> <http://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 <mailto: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 <mailto: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 <http://smtp.someserver.com>'
>     smtp_user = 'user at somedomain.com <mailto: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 <mailto:somebody at someplace.com>",
>     "FYI", "Here is something you really need to know:-------")
>
>
>     _______________________________________________
>     Triangle, NC Embedded Computing mailing list
>     TriEmbed at triembed.org <mailto: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/e4ee2233/attachment.htm>


More information about the TriEmbed mailing list