I am a new programmer that has begun learning code on python on raspberry pi 4 4gb. I am currently trying to get the Pi to send me an email that has an attached image. The code currently sends the text but will not send the file. I have attached the code that will send the email. Line 8 has my password for the email in the actual code, but I have replaced that for my accounts security. This code was written in python 3.7.3. Any help would be greatly appreciated.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'APResearch.test@gmail.com'
email_password = 'passsword goes here'
email_send = 'APResearch.test@gmail.com'
subject = 'subject'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body,'plain'))
filename= '/home/pi/Desktop/intruder.jpg'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('APResearch.test@gmail.com','apresearch1')
server.sendmail('APResearch.test@gmail.com','APResearch.test@gmail.com','motion detected')
server.quit()