# coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 发送纯文本格式的邮件
message = MIMEMultipart('related')
message.attach(MIMEText('there is some error or warning in your project', 'plain', 'utf-8'))
att1 = MIMEText(open('lintResult.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="aaaa"'
message.attach(att1)
#发送邮箱地址
sender = 'bbbb@cccc.com'
#登陆密码
password = 'eeee'
#收件箱地址
receiver = 'eeee@cccc.com, dddd@cccc.com'
#抄送
acc = 'eeee@cccc.com, dddd@cccc.com‘
#smtp服务器
smtp_server = 'ffff'
#发送邮箱地址
message['From'] = sender
#收件箱地址
message['To'] = receiver
message['Cc'] = acc
#主题
message['Subject'] = 'this is test email for gggg project'
server = smtplib.SMTP(smtp_server,25)
server.login(sender,password)
server.sendmail(sender,receiver,message.as_string())
server.quit()
很多企业内网或者外网邮箱都是通过邮箱服务器来实现收发邮件并不提供ssh接口,因此需要直联25端口来发送邮件,其中aaaa,bbbb,cccc等需要读者自行替换为所需的东东,其中有些换行或者空格是我后来加入的,可能会出错,需要读者重新替换下