I'm new to the RPi world, and as my first "dive" I chose to follow a couple of examples of sending emails through the console (running Raspberrian). More specifically, I've tried the example in the educational manual (p. 116, http://downloads.raspberrypi.org/Raspberry_Pi_Education_Manual.pdf) and here http://elinux.org/RPi_Email_IP_On_Boot_Debian. The latter is more relevant to what I'm trying to do, so I'll attach the code:
- Code: Select all
import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# Change to your own account information
to = 'me@example.com'
gmail_user = 'test@gmail.com'
gmail_password = 'yourpassword'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()
# Very Linux Specific
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src')+1]
my_ip = 'Your ip is %s' % ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'IP For RaspberryPi on %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
Now, when I try to run either one on IDLE, I get syntax error such as "% ipaddr." Then I remove the the code that gives errors (mainly the ip relevant things), and when it complies I get an error regarding the smtplib and MIMEText. Lastly, When I only type "import smtplib" in the python shell, it returns an error.
How can I fix this?
Thank you