[TriEmbed] Raspberry Pi and eMail
Pete Soper
pete at soper.us
Tue Nov 10 16:39:44 CST 2015
Thanks for all the info, guys!
TL;TD; I can send mail via my personal domain server and with a little
extra code I can avoid any worries about security (and I'm behind a
firewall anyway). So I'm covered and the rest of this is just for the
sake of helping others interested in using Gmail.
Gmail simply doesn't work for me. Since Adam and Brian have no trouble,
I have to assume it's something peculiar to my environment (AT&T DSL
with their approved Pace modem/router).
I tried Adam's code a second time and got an immediate "id/password
rejected" message. Then I created a virgin Gmail account and got this
very strange message with Adam's code:
psoper at len:~/p$ ./foo.py
To: fredfarkel at bitser.net
Subject: FYI
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 24, in send_msg
msg['From'] = email.utils.formataddr((_fromname, _username))
NameError: global name 'email' is not defined
I didn't realize it's possible to do an "un-import" in Python. In fact,
not being able to find 'email' in Adam's code is pretty disturbing, as
if the Python runtime has barfed on its shoes. The exact code I tried
with a few string characters mutated is at the end of this message in
case any Python wizards can spot something that would explain this (IMO
terrible) misbehavior.
The Nuts and Volts code works perfectly with my domain server account
(the mail supporting my usual address) and fails with a vanilla error
with both Gmail accounts. I feel it would be a silly waste of time to do
the package installs and configuration to try that other code Brian
pointed to. That approach is very interesting and I want to try it some
day, but I need to pop my stack now and move on.
-Pete
#!/usr/bin/python
import getpass, smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
_username = "aaabaaaaaaaaaaaaaaaaaaaaq"
_password = "raaaaaaaaafaaaaaaaaaaaaaa$"
_fromname = "Neco Cat"
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())
send_msg('fredfarkel at bitser.net', 'FYI', 'Here is a message')
More information about the TriEmbed
mailing list