使用Python发送邮件
环境Python 2.7.5CentOS Linux release 7.9.2009 (Core)代码# -*- coding:utf-8 -*-#!/usr/bin/env python# filename: xmail.py"""python xmail.py "all-sink-cnt-names.txt""xx全部souce connector名称""xys721@163.com"pyt
·
环境
Python 2.7.5
CentOS Linux release 7.9.2009 (Core)
代码
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# filename: xmail.py
"""
python xmail.py "all-sink-cnt-names.txt" "xx全部souce connector名称" "xys721@163.com"
python xmail.py 保存邮件内容的文件名 邮件标题 收件人
"""
import sys
import os
import json
# from datetime import datetime, timedelta
import datetime
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
# print sys.argv[1]
# ON open mail send otherwise OFF
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.set_debuglevel(1)
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"
更多推荐
已为社区贡献2条内容
所有评论(0)