Python 发送邮件模块
上一篇 /
下一篇 2017-08-04 17:38:23 / 天气: 晴朗
/ 心情: 高兴
/ 个人分类:Python
# coding: utf-8
import smtplib
from email.mime.text import MIMEText
import time
import urllib
import urllib2
from email import Utils
MAIL_SERVER = 'smtp邮件服务器'
MAIL_FROM = '发送者邮箱'
MAIL_LOGIN_PWD = '邮箱密码'
class send_email:
u'''
构造方法,初始化 发件人 & 收件人
'''
def __init__(self,
server=MAIL_SERVER, mail_from=MAIL_FROM,
loginpwd=MAIL_LOGIN_PWD):
self.server = server
self.mail_from = mail_from
self.loginpwd = loginpwd
def sendmail_by_python(self,mailbody,mailto):
msg = MIMEText(mailbody, _subtype='html', _charset='utf-8')
#定义标题
msg['Subject'] = '邮件标题'
#定义发送时间(不定义的可能有的邮件客户端会不显示发送时间)
msg['date'] = Utils.formatdate(localtime=1)
msg['to'] = ','.join(mailto)
msg['from'] = MAIL_FROM #"EpayTestCenter<ltzftesting@163.com>"
#连接SMTP 服务器
if 'smtp.qq.com' == self.server:
smtp = smtplib.SMTP_SSL(self.server)
smtp.ehlo(self.server)
else:
smtp = smtplib.SMTP()
smtp.connect(self.server)
# 用户名密码
smtp.login(self.mail_from, self.loginpwd)
smtp.sendmail(self.mail_from, mailto, msg.as_string())
smtp.quit()
收藏
举报
TAG: