Blog Content
使用SSL 465端口发送邮件的Python脚本
Python
2016-04-25 23:06:11
import time
import datetime
import smtplib
import os
import email.encoders as Encoders
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.Utils import COMMASPACE, formatdate, parseaddr, formataddr
from email.Header import Header
### define to mail
to_addrs = ['x6@163.com']
message = date + ' xxxx.message' + '''
__________________________________________
xxx团队
'''
subject = '信息|' + date
attachlist = [ storePath + x for x in uploadFile ]
#print attachlist
if attachlist:
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = formataddr(('ops', 'ops@bnw.com'))
msg['To'] = ', '.join(to_addrs)
msg['Date'] = formatdate(localtime=True)
html = True
if not html:
msg.attach(MIMEText(message, 'plain', 'utf-8'))
else:
msg.attach(MIMEText(message, 'html', 'utf-8'))
for f in attachlist:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
try:
s = smtplib.SMTP_SSL('smtp.mxhichina.com:465')
s.login('ops@bnw.com','bnw')
s.sendmail('ops@bnw.com',to_addrs, msg.as_string())
s.quit()
except Exception,e:
print(Exception,":",e)
print("Your email is sent successfully.")
else:
print("Error, Attach List Files Is Empty.")
上一篇:flask解决SQLAlchemy提示not bound to Session错误的问题session.merge
下一篇:pandas中计算总体标准差