这篇文章主要为大家详解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社区 其它相关文章!