Python:用attachemen发送电子邮件

2024-05-19 12:04:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个项目与树莓皮3,使一张照片时,有东西在移动。现在我想发一封附有这张照片的电子邮件。你知道吗

我已经有一个代码,但它花了30秒来发送邮件。 到目前为止,我掌握的代码是:

msg = MIMEMultipart()
msg['Subject'] = 'Bewegung erkannt!'+' '+timestr
msg['From'] = 'surveillance.picam@gmail.com'
msg['To'] = 'fuchsfan23@gmail.com'

body = email.mime.Text.MIMEText("""Bewegung erkannt! Hier ist der Eindrinling!""")
msg.attach(body)

directory = '/home/pi/Pictures/RaspCam/bild_{}.jpg'.format(timestr)

spl_dir=directory.split('/')

filename=spl_dir[len(spl_dir)-1]

spl_type=directory.split('.')

type=spl_type[len(spl_type)-1]
fp=open(directory,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype=type)
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)

s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('mailaddress1','passwd')
s.sendmail('mailaddress1','mailaddress2', msg.as_string())
s.quit()

这段代码在一个回调中,当有东西在移动时生成照片并保存它。你知道吗

我希望有人能帮我。。。 隐秘的


Tags: 代码comtypedirmsgfilenamespldirectory

热门问题