1、发送普通文本文件
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
host = 'smtp.126.com' # 设置发件服务器地址
port = 25 # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式
#发送邮箱
sender = '***@126.com'
#接收邮箱
receiver = '***@qq.com'
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126.com'
username = '***@126.com' #发送邮箱用户
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.connect('smtp.126.com',25)
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'], msg.as_string()) # 发送邮件!
print("邮件发送成功!")
2、发送邮件给多个人
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
host = 'smtp.126.com' # 设置发件服务器地址
port = 25 # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式
#发送邮箱
sender = '***@126.com'
#接收邮箱
receiver = ['***@qq.com','***@sina.com','***@126.com']
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126.com'
username = '***@126.com' #发送邮箱用户
password = '**************' #邮箱密码或授权码
#编写 text 类型的邮件正文
msg = MIMEText('<html><h1>比你更忙的人都在学习!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'wm8993@126.com'
msg['To'] =','.join(receiver)
smtp = smtplib.SMTP()
smtp.connect('smtp.126.com',25)
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) # 发送邮件!
print("邮件发送成功!")
3、发送邮件给多个人抄送给多个人
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
host = 'smtp.126.com' # 设置发件服务器地址
port = 25 # 设置发件服务器端口号。注意,这里有SSL和非SSL两种形式
#发送邮箱
sender = '***@126.com'
#接收邮箱
receiver = ['***@qq.com','***@sina.com','***@126.com']
cc = ['***@qq.com','***@sina.com','***@126.com']
#发送邮件主题
subject = 'Python email test'
#发送邮箱服务器
smtpserver = 'smtp.126.com'
username = '***@126.com' #发送邮箱用户
password = '***' #邮箱密码或授权码
#编写 text 类型的邮件正文
msg = MIMEText('<html><h1>比你更忙的人都在学习!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = 'wm8993@126.com'
msg['To'] =','.join(receiver)
msg['Cc'] =','.join(cc)
print(msg['To'])
smtp = smtplib.SMTP()
smtp.connect('smtp.126.com',25)
smtp.login(username, password) # 登陆邮箱
smtp.sendmail(msg['From'], msg['To'].split(',') + msg['Cc'].split(',') , msg.as_string()) # 发送邮件!
print("邮件发送成功!")
4、发送附件
#coding=utf-8
from email.mime.text import MIMEText#专门发送正文
from email.mime.multipart import MIMEMultipart#发送多个部分
from email.mime.application import MIMEApplication#发送附件
import smtplib#发送邮件
file = 'C:\\Users\\wm\\Desktop\\101.txt' #附件路径
file1 = 'C:\\Users\\wm\\Desktop\\102.txt' #附件路径
send_user = '***@126.com' #发件人
password = '*************' #授权码/密码
receive_users = '***@qq.com' #收件人,可为list
subject = 'Python email test' #邮件主题
email_text = '这是菜鸟教程Python 邮件发送测试……' #邮件正文
server_address = 'smtp.126.com' #服务器地址
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、发送图片
#coding=utf-8
from email.mime.text import MIMEText#专门发送正文
from email.mime.multipart import MIMEMultipart#发送多个部分
from email.mime.application import MIMEApplication#发送附件
from email.mime.base import MIMEBase
import smtplib#发送邮件
from email import encoders
file = 'C:\\Users\\wm\\Desktop\\101.txt' #附件路径
file1 = 'C:\\Users\\wm\\Desktop\\102.txt' #附件路径
send_user = '***@126.com' #发件人
password = '***************' #授权码/密码
receive_users = '***@qq.com' #收件人,可为list
subject = 'Python email test' #邮件主题
email_text = '这是菜鸟教程Python 邮件发送测试……' #邮件正文
server_address = 'smtp.126.com' #服务器地址
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) #添加附件
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_payload(f.read())
# 用Base64编码:
encoders.encode_base64(mime)
# 添加到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('邮件发送成功!')