詳解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學習者快速成長!