Python整理(发送邮件接口的使用)

接口:SendMail()

SendMail('rad_radisson@163.com', strBuildMail, 'radisson123', "BuildError--%s" %curBuildName, '请查看并及时修改!')
发方邮箱()可以为任意邮箱,收方邮箱,主题( BuildError--%s" %curBuildName,),内容(可以为附件,以路径形式)
gPathSrc =  u"/build_two/wanglong/RR512/512_t/trunk/code"


def SendMail(fromAddress, toAddress, usepassword,subject=None, content=None, attfile=None, subtype='plain', charset='utf-8'):
    username = fromAddress
    
    #创建一个带附件的实例
    msg = MIMEMultipart()
    msg['From'] = fromAddress  
    msg['To'] = toAddress  

    if subject:    
        #标题
        msg['Subject'] = subject
        msg['Date'] = Utils.formatdate(localtime=1)
    
    if content:
        #添加邮件内容
        txt = MIMEText(content, subtype, charset)  
        msg.attach(txt)  
        
    if attfile:
        #构造附件  
        #注意:传入的参数attfile为unicode,否则带中文的目录或名称的文件读不出来  
        #      basename 为文件名称,由于传入的参数attfile为unicode编码,此处的basename也为unicode编码  
        basename = os.path.basename(attfile)  
        #print basename
        #注意:指定att的编码方式为gb2312  
        att = MIMEText(open(attfile, 'rb').read(), 'base64', 'gb2312')  
        att["Content-Type"] = 'application/octet-stream'  
          
        #注意:此处basename要转换为gb2312编码,否则中文会有乱码。  
        #      特别,此处的basename为unicode编码,所以可以用basename.encode('gb2312')  
        #            如果basename为utf-8编码,要用basename.decode('utf-8').encode('gb2312')  
        att["Content-Disposition"] = 'attachment; filename=%s' % basename.encode('gb2312')  
        msg.attach(att)
    
    try:  
    #smtp = smtplib  
        smtp = smtplib.SMTP()  
        
        #连接服务器
        smtp.connect('smtp.163.com', '25')
        #登录
        smtp.login(username, usepassword)
        #发送邮件    
        smtp.sendmail(fromAddress, toAddress, msg.as_string())  
        #退出
        smtp.quit()  
        print('邮件发送成功email has send out !')  
    except Exception as e:
        print str(e)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值