整理一下,python 脚本发邮件的函数。
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
def sendMail():
'''
发送邮件
'''
# 发件人的邮箱账号
sender = "****@****"
# 发件人的邮箱密码
sender_password = "****"
# 收件人的邮箱账号
receiver_mail_adress = ["****@****"]
# 抄送人的邮箱账号
cc_mail_address = ["****@****", "****@****", "****@****", "****@****"]
#邮件正文
body = '''
****
'''
msg = MIMEMultipart()
#邮件主题
msg["Subject"] = Header("***", "utf-8")
#邮件发件人
msg["From"] = Header("***", "utf-8")
#邮件收件人
msg["To"] = Header("***", 'utf-8')
#邮件抄送
msg['Cc'] = ','.join([u"**", u"**", u"**", u"**"])
#邮件正文
msg.attach(MIMEText(body, 'html', 'utf-8'))
# 附件
att1 = MIMEText(open("********", 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# filename 我写成中文就乱码,还是先英文
att1["Content-Disposition"] = 'attachment; filename= ****'
msg.attach(att1)
# 发送邮件
try:
server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
server.login(sender, sender_password)
server.sendmail(sender, receiver_mail_adress + cc_mail_address, msg.as_string())
server.quit()
except smtplib.SMTPException as e:
raise e
if __name__ == "__main__":
sendMail()