So, I have an Arduino Yun, and I'm trying to send an email with python. I have this code that I got from https://github.com/amicojeko/YouCantTouchThis
But when I try to call the file with $python sendemail.py test.png
, it returns me an error:
ImportError: No module named mime.multipart
I already tried to change to lowercase the imports, but it keeps showing the same error. The python version in the Arduino is 2.7. I've read something about module reorganization but even with the changes the error keep showing.
Here is the code:
# coding=utf-8
# Copyright (C) 2014 Stefano Guglielmetti
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import smtplib, os, sys
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
#From address, to address, subject and message body
from_address = '[email protected]'
to_address = ['[email protected]']
email_subject = 'Alert!!! Zombies!!! Ahead!!!'
email_body = 'A non dead intruder has been detected and needs to be eliminated!'
# Credentials (if needed)
username = 'EMAIL_LOGIN'
password = 'EMAIL_PASSWORD'
# The actual mail send
server = 'smtp.gmail.com:587'
def send_mail(send_from, send_to, subject, text, files=[], server="localhost"):
assert type(send_to)==list
assert type(files)==list
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
smtp = smtplib.SMTP(server)
smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
send_mail(from_address, to_address, email_subject, email_body, [sys.argv[1]], server) #the first command line argument will be used as the image file name
-
post the complete traceback i.e. the error.SoreDakeNoKoto– SoreDakeNoKoto2016年04月16日 05:14:20 +00:00Commented Apr 16, 2016 at 5:14
1 Answer 1
mime.multipart
isnt used in your code at all so you shouldn't be getting that error. The correct import statements for Python 2.7 are:
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email.encoders import Encoders
Study these examples in the Python 2.7 documentation.
-
Ok, I've done this and it worked perfectly. I had a little trouble with the Gmail server after on but it was a simple trick.Guilherme Raguzzoni– Guilherme Raguzzoni2016年04月17日 22:39:41 +00:00Commented Apr 17, 2016 at 22:39