使用SSL 465端口发送邮件的Python脚本

有些时候使用smtp 25端口发mail安全性不大保障,另外还有mail厂商直接禁用smtp 25端口发送邮件,所以妥一点还是使用smtp SSL 加密协议发邮件吧。所以现在就分享一个可以使用SSL 465端口发邮件的Python脚本,有需要的朋友可以直接拿去。

Python代码如下:

#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-
### author by 天擎
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@bnw.com']
message = date + ' xxxx.message' +  '''
</br>
</br>
<span style="font-family: Arial;color:RGB(128,128,128)">  
__________________________________________<br>
<b>  
<i>xxx团队</i><br>  
</b>  
</span>
          '''
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:
        print f
        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."
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容