注意:部分文章发布时间较长,可能存在未知因素,购买时建议在本站搜索商家名称,先充分了解商家动态。
交流:唯一投稿邮箱:hostvps@88.com。
现在一般的服务商默认禁止25端口但没有禁止465端口,如果需要使用25端口发送邮件需单独或自助申请,我可不想把功夫花在这上面。#!/usr/bin/env python#-*- coding:utf-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.utils import formatdatefrom email.header import Headerimport sys# 设置默认字符集为UTF8 不然有些时候转码会出问题default_encoding = ‘utf-8’if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding)# 发送邮件的相关信息,根据你实际情况填写smtpHost = ‘smtp.163.com’smtpPort = ’25’sslPort = ‘465’fromMail = ‘xxx@163.com’# toMail = ‘xxx@qq.com’username = ‘xxx@163.com’password = ‘password’def send_mail(to_list, subject, content): # 邮件标题和内容 # subject = u'[Notice]hello’ # body = u’hello,this is a mail from ‘ + fromMail # 初始化邮件 encoding = ‘utf-8’ mail = MIMEText(content.encode(encoding), ‘plain’, encoding) mail[‘Subject’] = Header(subject, encoding) mail[‘From’] = fromMail mail[‘To’] = to_list mail[‘Date’] = formatdate() try: # 连接smtp服务器,明文/SSL/TLS三种方式,根据你使用的SMTP支持情况选择一种 # 普通方式,通信过程不加密 # smtp = smtplib.SMTP(smtpHost, smtpPort) # smtp.ehlo() # smtp.login(username, password) # tls加密方式,通信过程加密,邮件数据安全,使用正常的smtp端口 # smtp = smtplib.SMTP(smtpHost,smtpPort) # smtp.set_debuglevel(True) # smtp.ehlo() # smtp.starttls() # smtp.ehlo() # smtp.login(username,password) # 纯粹的ssl加密方式,通信过程加密,邮件数据安全 smtp = smtplib.SMTP_SSL(smtpHost,sslPort) smtp.ehlo() smtp.login(username,password) # 发送邮件 smtp.sendmail(fromMail, to_list, mail.as_string()) smtp.close() print(“OK”) except Exception as e: print(“e”)send_mail(sys.argv[1], sys.argv[2], sys.argv[3])附一个cqa qq群告警脚本,告警信息发Q群方便处理:#!/usr/bin/env python# -*- coding:utf-8 -*-#Ken 2017/07/21import sys import urllibimport urllib2d1 = sys.argv[1]d2 = sys.argv[3]test_data = {‘group_id’:d1,’message’:d2}test_data_urlencode = urllib.urlencode(test_data)requrl = “http://你的接口上报地址:5700/send_group_msg”req = urllib2.Request(url = requrl,data =test_data_urlencode)res_data = urllib2.urlopen(req)res = res_data.read()print resserver酱告警脚本,虽然不够实用但是还是贴上:#!/bin/shPATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin:$HOME/bin:/usr/local/bin:/usr/binexport PATHexport LANG=en_US.UTF-8obj=”$1″tt=”$2″mesg=”$3″nohup curl -d “text=${tt}&desp=${mesg}” http://sc.ftqq.com/${obj}.send >/dev/null 2>&1 &