python中发送邮件(普通⽂本⽂件、附件、图⽚等)1、发送普通⽂本⽂件
#coding=utf-8
import smtplib
from import MIMEText
from email.header import Header
host = 'smtp.126' # 设置发件服务器地址
port = 25 # 设置发件服务器端⼝号。注意,这⾥有SSL和⾮SSL两种形式
#发送邮箱
sender = '***@126'
#接收邮箱
receiver = '***@qq'
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126'
username = '***@126' #发送邮箱⽤户
password = '**************' #邮箱密码或授权码
#编写 text 类型的邮件正⽂
#msg = MIMEText('<html><h1>⽐你更忙的⼈都在学习!</h1></html>','html','utf-8')
msg = MIMEText('⽐你更忙的⼈都在学习!111','plain','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP()
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'], msg.as_string()) # 发送邮件!
print("邮件发送成功!")
2、发送邮件给多个⼈
host = 'smtp.126' # 设置发件服务器地址
port = 25 # 设置发件服务器端⼝号。注意,这⾥有SSL和⾮SSL两种形式
#发送邮箱
sender = '***@126'
#接收邮箱
receiver = ['***@qq','***@sina','***@126']
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126'
username = '***@126' #发送邮箱⽤户
password = '**************' #邮箱密码或授权码
#编写 text 类型的邮件正⽂
msg = MIMEText('<html><h1>⽐你更忙的⼈都在学习!</h1></html>','html','utf-8') msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'wm8993@126'
msg['To'] =','.join(receiver)
smtp = smtplib.SMTP()
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) # 发送邮件!print("邮件发送成功!")
3、发送邮件给多个⼈抄送给多个⼈
host = 'smtp.126' # 设置发件服务器地址
port = 25 # 设置发件服务器端⼝号。注意,这⾥有SSL和⾮SSL两种形式
#发送邮箱
sender = '***@126'
#接收邮箱
receiver = ['***@qq','***@sina','***@126']
cc = ['***@qq','***@sina','***@126']
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126'
username = '***@126' #发送邮箱⽤户
password = '***' #邮箱密码或授权码
#编写 text 类型的邮件正⽂
msg = MIMEText('<html><h1>⽐你更忙的⼈都在学习!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'wm8993@126'
msg['To'] =','.join(receiver)
msg['Cc'] =','.join(cc)
print(msg['To'])
smtp = smtplib.SMTP()
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'].split(',') + msg['Cc'].split(',') , msg.as_string()) # 发送邮件!print("邮件发送成功!")
4、发送附件
import smtplib#发送邮件
file = 'C:\\Users\\wm\\Desktop\\' #附件路径
file1 = 'C:\\Users\\wm\\Desktop\\' #附件路径
send_user = '***@126' #发件⼈
password = '*************' #授权码/密码
receive_users = '***@qq' #收件⼈,可为list
subject = 'Python email test' #邮件主题
email_text = '这是菜鸟教程Python 邮件发送测试……' #邮件正⽂
server_address = 'smtp.126' #服务器地址
mail_type = '1' #邮件类型
#构造⼀个邮件体:正⽂附件
msg = MIMEMultipart()
msg['Subject']=subject #主题
msg['From']=send_user #发件⼈
msg['To']=receive_users #收件⼈
#构建正⽂
part_text=MIMEText(email_text)
msg.attach(part_text) #把正⽂加到邮件体⾥⾯去
#构建邮件附件
#file = file #获取⽂件路径
part_attach1 = MIMEApplication(open(file,'rb').read()) #打开附件
part_attach1.add_header('Content-Disposition','attachment',filename=file) #为附件命名msg.attach(part_attach1) #添加附件
# 发送邮件 SMTP
smtp= smtplib.SMTP(server_address,25) # 连接服务器,SMTP_SSL是安全传输
smtp.login(send_user, password)
smtp.sendmail(send_user, receive_users, msg.as_string()) # 发送邮件
print('邮件发送成功!')
4、发送图⽚
from email.mime.base import MIMEBase
import smtplib#发送邮件
from email import encoders
file = 'C:\\Users\\wm\\Desktop\\' #附件路径
file1 = 'C:\\Users\\wm\\Desktop\\' #附件路径
send_user = '***@126' #发件⼈
password = '***************' #授权码/密码
receive_users = '***@qq' #收件⼈,可为list
subject = 'Python email test' #邮件主题
email_text = '这是菜鸟教程Python 邮件发送测试……' #邮件正⽂
server_address = 'smtp.126' #服务器地址
mail_type = '1' #邮件类型
#构造⼀个邮件体:正⽂附件
msg = MIMEMultipart()
msg['Subject']=subject #主题
菜鸟教程python2msg['From']=send_user #发件⼈
msg['To']=receive_users #收件⼈
#构建正⽂
part_text=MIMEText(email_text)
msg.attach(part_text) #把正⽂加到邮件体⾥⾯去
#构建邮件附件
#file = file #获取⽂件路径
part_attach1 = MIMEApplication(open(file,'rb').read()) #打开附件
part_attach1.add_header('Content-Disposition','attachment',filename=file) #为附件命名msg.attach(part_attach1) #添加附件
with open('C:\\Users\\wm\\Desktop\\tp.png', 'rb') as f:
# 设置附件的MIME和⽂件名,这⾥是png类型:
mime = MIMEBase('image', 'png', filename='tp.png')
# 加上必要的头信息:
mime.add_header('Content-Disposition', 'attachment', filename='tp.png')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的内容读进来:
mime.set_ad())
# ⽤Base64编码:
# 添加到MIMEMultipart:
msg.attach(mime)
#正⽂显⽰附件图⽚
msg.attach(MIMEText('<html><body><h1>Hello</h1>' +
'<p><img src="cid:0"></p>' +
'</body></html>', 'html', 'utf-8'))
# 发送邮件 SMTP
smtp= smtplib.SMTP(server_address, 25) # 连接服务器,SMTP_SSL是安全传输
smtp.login(send_user, password)
smtp.sendmail(send_user, receive_users, msg.as_string()) # 发送邮件
print('邮件发送成功!')
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论