python实现smtp发送邮件类-直接调用就好

python实现smtp发送邮件类

使用方法

#使用方法Sendmail('stmp服务器', '邮箱', '密码', 发送的邮箱用list格式,可多个发送)
p = Sendmail('smtp.minshengec.cn', '[email protected]', 'xxoofor you', ['[email protected]'])
#setMailInfo('标题', '内容')
p.setMailInfo('dsadsa', 'xxoo')
#最后运行run方法
p.run()
#!/usr/bin/env python
# -*- coding: gbk -*-
#导入smtplib和MIMEText

import smtplib
from email.mime.text import MIMEText

class Sendmail(object):

    def __init__(self, mailHost, mailUser, mailPwd):

        self.mailHost = mailHost;
        self.mailUser = mailUser;
        self.mailPwd = mailPwd;
        self.sendList = [];

#function subject->title
    def setMailInfo(self, sendList,subject, content):
        self.sendList = sendList
        self.content = content;
        self.subject = subject;
        self.msg = MIMEText(self.content)
        self.msg['From'] = self.mailUser
        self.msg['Subject'] = self.subject
        self.msg['To'] = ";".join(self.sendList)
    def run(self):
        try:
            self.send = smtplib.SMTP()
            self.send.connect(self.mailHost)
            self.send.login(self.mailUser, self.mailPwd)
            self.send.sendmail(self.mailUser, self.sendList, self.msg.as_string())
            print '[*]-----send mail---to' + str(self.sendList) + 'success-----[*]'
        except smtplib.SMTPException as e:
            print e

#p = Sendmail('smtp.minshengec.cn', '[email protected]', 'xxoo for you')
#p.setMailInfo(['[email protected]'], 'dsadsaqqq', 'xxodsado')
#p.run()

python实现smtp发送邮件类-直接调用就好_第1张图片

你可能感兴趣的:(编程之美)