jeremy
jeremy

Reputation: 11

Error when attaching MIME on python script

The purpose of this script is to listen on serial port and send an email with a ip camera picture which is retrieved when its triggered. Here is what I have so far with error.

` import time import serial from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import smtplib import datetime import urllib

TO = '[email protected]'
GMAIL_USER = '[email protected]'
GMAIL_PASS = 'password'

SUBJECT = 'Intrusion= NODE 18'
TEXT = 'MOVEMENT'

ser = serial.Serial('/dev/tty.usbserial-DA00S0U6', 115200)

def send_email():
urllib.urlretrieve ("http://<IPADDRESS>/snapshot/view4.jpg","view4.jpg")
print("Sending Email")
print str(datetime.datetime.now())
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(GMAIL_USER, GMAIL_PASS)
header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
header = header + '\n' + 'Subject:' + SUBJECT + '\n'
print header
msg = MIMEMultipart() + header + '\n' + TEXT + ' \n\n'
msg.attach(MIMEImage(file("view4.jpg").read()))
smtpserver.sendmail(GMAIL_USER, TO, msg)
smtpserver.close()



 while True:
 message = ser.readline()
 print(message)
 if "[18] MOTION" in message :
     send_email()

 time.sleep(0.5)`

ERROR:===

Traceback (most recent call last): File "motion3.py", line 58, in <module> send_email() File "motion3.py", line 32, in send_email msg = MIMEMultipart() + header + '\n' + TEXT + ' \n\n' TypeError: unsupported operand type(s) for +: 'instance' and 'str'

Upvotes: 1

Views: 362

Answers (1)

PM 2Ring
PM 2Ring

Reputation: 55469

In future, please make sure that the code you post is indented properly!

You're not using the MIMEMultipart class properly. Please see the email: Examples in the Python docs; the 3rd example shows the proper usage of MIMEMultipart.

The error message is saying that you are trying to add together a new class instance MIMEMultipart() and a string header, and you can't do that!

There may be logic errors in your code, but I haven't looked very closely at it - it's just too painful trying to read badly-indented Python. :(

Upvotes: 1

Related Questions