python 发送邮件基础

python 发送邮件基础代码

import datetime
import email
import shutil
import os
from email.mime.application import MIMEApplication
import smtplib as sm
import email.mime.multipart
import email.mime.text

current_date = datetime.date.today()- datetime.timedelta(days=1)
msg=email.mime.multipart.MIMEMultipart()
msg[‘to’]=’接受邮箱’
msg[‘Cc’]=’抄送邮箱’
msg[‘from’]=’发送邮箱’
msg[‘subject’]=’附件名’
content=’主题’+str(current_date)
content=email.mime.text.MIMEText(content)
msg.attach(content)

txt=MIMEApplication(open(‘文件路径’,’rb’).read())
txt[“Content-Type”] = ‘application/octet-stream’
txt.add_header(‘Content-Disposition’, ‘attachment’, filename=”文件名称”)
msg.attach(txt)

smtpObj = sm.SMTP_SSL(‘smtp.exmail.qq.com’, 465)
type(smtpObj)
smtpObj.ehlo()
smtpObj.login(‘发送邮箱’,’发送邮箱密码’)
smtpObj.sendmail(msg[‘from’],msg[‘Cc’].split(‘,’)+msg[‘to’].split(‘,’),msg.as_string())
smtpObj.quit()

shutil.rmtree(‘文件路径’) ————删除文件夹
os.mkdir(‘文件路径’) ————创建文件夹

猜你喜欢

转载自blog.csdn.net/weixin_34280060/article/details/82688235
0条评论
添加一条新回复