Python自动发动邮件

 

import smtplib                       #发送邮件模块
from email.mime.text import MIMEText #定义邮件内容
from email.header import Header      #定义邮件标题

#发送邮件服务器
smtpserver='smtp.163.com'

#发送邮箱用户名和密码
user='test_pythonmail@163.com'
password='cao15036214043'

#发送和接收邮箱
sender='test_pythonmail@163.com'
receive='1219853367@qq.com'

# 发送邮件主题和内容
subject='Web Selenuim 自动化测试报告'
content='<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'

#HTML邮件正文
msg=MIMEText(content,'html','utf-8')
msg['subject']=Header(subject,'utf-8')
msg['From']='test_pythonmail@163.com'
msg['To']='1219853367@qq.com'

#SSL协议端口号要使用465
smtp=smtplib.SMTP_SSL(smtpserver,465)

#向用户标识用户身份
smtp.helo(smtpserver)
#服务器返回结果确认
smtp.ehlo(smtpserver)
#登录邮箱服务器用户名和密码
smtp.login(user,password)


print("Start send Email....")
smtp.sendmail(sender,receive,msg.as_string())
smtp.quit()
print("Send Email end!")

邮件群发

import smtplib
from email.mime.text import MIMEText
from email.header import Header

smtpserver='smtp.163.com'

user='test_pythonmail@163.com'
password='cao15036214043'

sender='test_pythonmail@163.com'
receives=['1*******@qq.com','Ca*****ng@zhan.com']

subject='Web Selenuim 自动化测试报告'
content='<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'

msg=MIMEText(content,'html','utf-8')
msg['subject']=subject
msg['From']=sender
msg['To']=','.join(receives)

smtp=smtplib.SMTP_SSL(smtpserver,465)

smtp.helo(smtpserver)
smtp.ehlo(smtpserver)
smtp.login(user,password)

print("Start send Email....")
smtp.sendmail(sender,receives,msg.as_string())
smtp.quit()
print("Send Email end!")

邮件带附件群发

import smtplib
from email.mime.text import MIMEText#定义邮件内容
from email.mime.multipart import MIMEMultipart#用于传送附件

smtpserver='smtp.163.com'#发送邮箱服务器

user='test_pythonmail@163.com'
password='cao15036214043'

sender='test_pythonmail@163.com'
receive=['Cao****ng@zhan.com','1219***@qq.com']

#邮件的主题和内容
subject='Web Selenuim 自动化测试报告'
content='<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'
#构造附件内容
send_file=open(r"E:\logo.jpg",'rb').read()

att=MIMEText(send_file,'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="logo.jpg"'
#构建发送与接收信息
msgRoot=MIMEMultipart()
msgRoot.attach(MIMEText(content,'html','utf-8'))
msgRoot['subject']=subject
msgRoot['From']=sender
msgRoot['To']=','.join(receive)
msgRoot.attach(att)

smtp=smtplib.SMTP_SSL(smtpserver,465)

smtp.helo(smtpserver)
smtp.ehlo(smtpserver)
smtp.login(user,password)

print("Start send Email....")
smtp.sendmail(sender,receive,msgRoot.as_string())
smtp.quit()
print("Send Email end!")

 

posted @ 2017-11-20 16:35  小青~  阅读(153)  评论(0编辑  收藏  举报