I can't figure out why this code (which used to work and sends me an email with an image attachment) stopped working. I'm running it on a raspberry pi. It seems to run until it gets to the server.sendmail
line and then gives this error:
smtplib.SMTPServerDisconnected: Server not connected
I am sure that it used to work a few months ago the last time I tried it and I also tried it using a gmail account and smtp.gmail.com instead of my Dreamhost smtp server and got the same error. The code is:
def email(kid):
fromaddr = "me@email.com"
toaddr = "me@email.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = kid + " Button Pressed"
body = kid + " pressed the button"
msg.attach(MIMEText(body, 'plain'))
filename = "image.jpg"
attachment = open("/home/pi/python/image.jpg", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('mail.domain.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
The problem with Gmail (and many others mail services) might be with the security, check this:
https://support.google.com/accounts/answer/6010255?hl=en
You can change account access for less secure apps or enable the 2 step verification.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.