详解python之用smtplib模块实现第三方smtp发送邮的实例

高洛峰
发布: 2017-03-26 17:57:16
原创
997 人浏览过

这篇文章主要为大家详解python之用smtplib模块实现第三方smtp发送邮的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

#_*_ coding:utf-8 _*_
import  smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
class Sendmail:
 
    local_hostname = ['toby-ThinkPad-T430shhhh']
    msg = MIMEMultipart('related')
 
    def __init__(self,smtp_server,mail_user,mail_pass):
        self.smtp_server = smtp_server
        self.mail_user = mail_user
        self.mail_pass = mail_pass
 
    def mess(self,theme,message):
        Sendmail.msg['Subject'] = theme  # 邮件主题
        html_msg = '''
                
                

%s

''' % message html = MIMEText(html_msg, 'html', 'utf-8') Sendmail.msg.attach(html) def files(self,path,filenames): files = path + filenames att = MIMEText(open(files, 'rb').read(), 'base64', 'utf-8') att["Content-Type"] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename=%s' % filenames Sendmail.msg.attach(att) def send(self,receiver): smtp = smtplib.SMTP() smtp.connect(self.smtp_server) smtp.ehlo(self.local_hostname) # 使用ehlo指令向smtp服务器确认身份 smtp.starttls() # smtp连接传输加密 smtp.login(self.mail_user, self.mail_pass) smtp.sendmail(self.mail_user, receiver, Sendmail.msg.as_string()) smtp.quit() if __name__ == "__main__": a = Sendmail('xxxx.xxxx.com','xxxxx@xxxx.com','xxxxxx') #实例化一个发送邮件的对象 a.mess('hello world','this is test mail') #定义主题,消息 a.files('/var/log/','syslog.2.gz') #这是发送邮件 定义路径、文件名 a.send('xxxx@qq.com') #收件人
登录后复制

以上是详解python之用smtplib模块实现第三方smtp发送邮的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!