smtplib

作者: shuff1e | 来源:发表于2018-03-20 22:09 被阅读7次
#!/usr/bin/env python                                                                                                                         
# -*- coding: utf-8 -*-

import smtplib,datetime,time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.header import Header

class Email(object):
    def __init__(self,email,project):
        self.email=email
        self.project=project

    def sendEmail(self):
        stmphost='127.0.0.1'
        stmpport=25
        smtp=smtplib.SMTP(smtphost,smtpport)
        username='shuffle'
        password='123'
        smtp.login(username,password)

        msg=MIMEMultipart('alternative')
        msg['uname']=username
        msg['password']=password

        if len(self.email) > 1:
            msg['To']=','.join(self.email)
        else:
            msg['To']=self.email[0]
        msg['From']='shuffle@example.com'
        cc=[u'ChaoSong@qq.com']
        msg['Cc']=','.join(cc)

        html="""
        %s
        """ % (self.project,)
        part=MIMEText(html,'html','utf-8')
        msg.attach(part)

        subject='hello world!'
        msg['subject']=Header(subject,'utf-8')

        smtp.sendmail(msg['From'],self.email+cc,msg.as_string())
        smtp.quit()

if __name__=='__main__':
    try:
        email=Email('ShouJianRen@qq.com',u'你好')
        email.sendEmail()
    except Exception,e:
        print e                  

相关文章

网友评论

      本文标题:smtplib

      本文链接:https://www.haomeiwen.com/subject/dcyffftx.html