import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
class SendMail:
def __init__(self, path, sender, receiver):
self.sender_act = sender['sender']
self.password = sender['password']
self.receiver = receiver
self.path = path
try:
self.file_list = os.listdir(self.path)
except Exception as e:
self.file_list =[self.path]
self.smtp_server = smtplib.SMTP_SSL('smtp.qq.com')
def create_email(self):
self.msg = MIMEMultipart('alternative')
self.msg['Subject'] = "每日报表"
self.msg['From'] = self.sender_act
self.msg['To'] = ','.join(self.receiver)
for file in self.file_list:
self.msg.attach(self.add_attachment(file))
self.msg.attach(self.add_content())
return self.msg
def add_attachment(self, file):
att_file = MIMEText(
open(self.path + '/' + file, 'rb').read(),
'base64', 'gbk')
att_file["Content-Type"] = 'application/octet-stream'
att_file.add_header(
'Content-Disposition',
'attachment', filename=('gbk', '', file))
return att_file
def add_content(self):
html = '<html><body><p>您好,这是昨日报表,请您查阅。</p></body></html>'
att_text = MIMEText(html, 'html')
return att_text
def send_email(self):
msg = self.create_email()
with self.smtp_server as conn:
conn.login(self.sender_act, self.password)
conn.sendmail(self.sender_act, self.receiver, msg.as_string())
print('邮件发送成功')
用python发邮件
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 去年暑假,实验室和中国联合航空公司签了一个合同,由我们帮他们做一套旅游网站。现在项目已经上线数月,前一阵联航那边人...