-
简介:
- python专门发送email脚本,可以直接引用,命令行运行
-
应用场景:
- Linux中结合shell脚本通过命令行传参的形式发送邮件;
-
功能简介:
- 1.支持自定义标题、正文
- 2.支持命令行传参,支持获取帮助信息
-
Email_Tool.py
# coding=utf-8
__author__ = 'xcma'
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib, getopt, sys
def send_emal(subject, content):
# 邮件基础信息
smtpserver = 'smtp.XX.com'
smtpuser = 'XX@XX.com'
password = 'XXX'
mailto = ["XX"]
Subject = subject
content = content
msg = MIMEMultipart()
msg['From'] = smtpuser
msg['To'] = ",".join(mailto)
msg['Subject'] = Header(Subject, 'utf-8').encode()
msg["Accept-Language"] = "zh-CN"
msg["Accept-Charset"] = "ISO-8859-1,utf-8"
msg.attach(MIMEText(content, 'html', 'utf-8'))
# 发送邮件
server = smtplib.SMTP()
try:
# 不加密形式
server.connect(smtpserver, '25')
except:
# 通过ssl形式发送邮件
server = smtplib.SMTP_SSL(smtpserver, '465')
# 邮件日志
print (server.set_debuglevel(1))
server.login(smtpuser, password)
server.sendmail(smtpuser, mailto, msg.as_string())
server.quit()
def usage():
print ("""
参数使用说明:
-x [ subject ]*** 必填项
-y [ content ]*** 选填项
eg: python Email_Tool.py -x 1 -y 2
""")
if __name__ == '__main__':
subject = ''
content = ''
opts, args = getopt.getopt(sys.argv[1:], 'hx:y:')
for op, value in opts:
if op == '-x':
subject = value
elif op == '-h':
usage()
sys.exit()
for op, value in opts:
if op == '-y':
content = value
elif op == '-h':
usage()
sys.exit()
if subject != '':
send_emal(subject, content)
-
总结 :
- 超独立的小工具,只需要配置下发送邮箱即可,有需要的同学拿来就用岂不快哉!!!
@晴-2016-10-17 18:28:00