环境
Python 2.7.5
CentOS Linux release 7.9.2009 (Core)
代码
"""
python xmail.py "all-sink-cnt-names.txt" "xx全部souce connector名称" "xys721@163.com"
python xmail.py 保存邮件内容的文件名 邮件标题 收件人
"""
import sys
import os
import json
import datetime
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
MAIL_SWITCH = "ON"
def xsend_mail(subject, mail_msg, rec="xys721@163.com"):
if MAIL_SWITCH == "OFF":
return
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
to_addr = rec
from_addr = 'qstest@163.com'
password = 'qstest2019'
smtp_server = 'smtp.163.com'
msg = MIMEText(mail_msg, 'plain', 'utf-8')
msg['From'] = _format_addr('<%s>' % from_addr)
msg['To'] = _format_addr('<%s>' % to_addr)
msg['Subject'] = Header(subject, 'utf-8').encode()
server = smtplib.SMTP_SSL(smtp_server, 465)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr.split(','), msg.as_string())
server.quit()
with open(sys.argv[1], 'r') as fb:
xmail_content = fb.read()
xsend_mail(sys.argv[2], xmail_content, sys.argv[3])
演示效果
echo "hello-world 邮件测试内容" > all-sink-cnt-names.txt
python xmail.py "all-sink-cnt-names.txt" "xx全部souce connector名称" "xys721@163.com"
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/57e24b3381ab46bc6c6acbdd7aefa5c5.png)