python发送邮件脚本记录
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def Mailer(to_list, th1=None, Subject=None, unipath=None):
mail_host = 'smtp.qq.com' # 邮箱服务器
mail_user = ' ......' # 发件人邮箱密码(当时申请smtp给的口令)
mail_pwd = '......' # SMTP密码
s = smtplib.SMTP_SSL(mail_host, 465, timeout=5)
s.login(mail_user, mail_pwd)
mail = str(th1)
msg = MIMEMultipart()
msgtext = MIMEText(mail.encode('utf8'), _subtype='html', _charset='utf8')
msg['From'] =u'测试机 <%s>'%mail_user
msg['Subject'] = Subject
msg['To'] = ",".join(to_list)
if unipath is not None:
att1 = MIMEText(open(unipath, 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1.add_header('Content-Disposition', 'attachment', filename='LoginServerInfo.xls')
msg.attach(att1)
msg.attach(msgtext)
try:
s.sendmail(mail_user, to_list, msg.as_string())
s.close()
print('发送成功')
except Exception as e:
print(e)
to_list = [
'zhangxuyang_m@cyou-inc.com',
]
Mailer(to_list, th1="LoginServerInfo.txt", Subject='【配置文件】', unipath=r'/opt/xuyang/shujuchuli/infodir/newdate.xls')