关于Flask 邮件发送,支持多人发送

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header


class SendEmail(object):
def __init__(self, mailserver, username_send, password, port, server_type):
self.mailserver = mailserver # 邮箱服务器地址
self.username_send = username_send # 邮箱用户名
self.password = password # 邮箱密码:需要使用授权码
self.port = port
self.server_type = server_type


def make_mail(self):
self.mail = MIMEText(self.content, 'plain', 'utf-8')
self.mail['Subject'] = Header(self.header, 'utf-8')
self.mail['From'] = self.username_send # 发件人
self.mail['To'] = ",".join(self.msg_to)    # 收件人;[]里的三个是固定写法,别问为什么,我只是代码的搬运工

def send_email(self,username_recv, content, header):
self.msg_to = username_recv.split(",")
# print self.msg_to
self.content = content
self.header = header
self.username_recv = username_recv # 收件人,多个收件人用逗号隔开
self.make_mail()

try:
smtp = smtplib.SMTP(self.mailserver, self.port) # 连接邮箱服务器,smtp的端口号是25
smtp.login(self.username_send, self.password) # 登录邮箱
smtp.sendmail(self.username_send, self.msg_to,
self.mail.as_string()) # 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit()
return True
except smtplib.SMTPException:
return False


if __name__ == '__main__':
a = SendEmail( "smtp.163.com", "[email protected]", "******* ", 25, "SMTP")
print a.send_email("[email protected]",".这是测试内容","邮件标题")

# print "success..."

猜你喜欢

转载自www.cnblogs.com/Adalia-Ting/p/11428487.html