Python发送邮件的例子

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/marising/article/details/49275683
import base64, smtplib
from email.mime.text import MIMEText
from email.header import Header

def mail_info(data, date):
    mailserver = "smtp.exmail.qq.com"
    username = "server@xxx.com"
    password = "xxx"
    from_addr = "server@xxx.com"
    to_addr = []
    to_addr.append('diablo@xxx.com')
    to_addr.append('abc@xxx.com')

    msg = MIMEText( data,'html','utf-8') #Html邮件,文本为text
    msg['Subject'] = Header('[MinuteReport]%s 15 Minute Business data' % date )
    msg['From'] = from_addr
    msg['To'] = ','.join(to_addr) #这里是收件人展示用,字符串
    svr = smtplib.SMTP(mailserver)
    #svr.set_debuglevel(1)        #debug模式打开,会有调试信息
    code = svr.ehlo()[0]
    if not (200 <= code <=299):
        code = svr.helo()[0]
        if not (200 <= code <=299):
            print 'hello error'
            return
    if svr.has_extn('starttls'):  //tls模式
        svr.starttls()
        code = svr.ehlo()[0]
        if not (200 <= code <=299):
            print 'ttls error'
            return
    if svr.has_extn('auth'):    //用户名和密码认证
        svr.login( username, password )
    svr.sendmail( from_addr, to_addr, msg.as_string() ) #to_addr,这里用List
    svr.quit()

阅读更多
换一批

没有更多推荐了,返回首页