我如何让Raspberry Pi用python发送带有图片的电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何让Raspberry Pi用python发送带有图片的电子邮件相关的知识,希望对你有一定的参考价值。

我是一名新程序员,已经开始在raspberry pi 4 4gb上使用python学习代码。我目前正在尝试让Pi给我发送一封带有附件图像的电子邮件。该代码当前发送文本,但不会发送文件。我已经附上了将发送电子邮件的代码。第8行在实际代码中包含了我用于电子邮件的密码,但是为了我的帐户安全,我已替换了该密码。这段代码是用python 3.7.3编写的。任何帮助将不胜感激。

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()
答案

我也需要这个答案!请帮忙!任何帮助表示赞赏。

以上是关于我如何让Raspberry Pi用python发送带有图片的电子邮件的主要内容,如果未能解决你的问题,请参考以下文章