严选品质
正规商家

云服务商禁止25端口该如何发送邮件

现在一般的服务商默认禁止25端口但没有禁止465端口,如果需要使用25端口发送邮件需单独或自助申请,我可不想把功夫花在这上面。

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header
import 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/21

import sys 
import urllib
import urllib2

d1 = 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 res

server酱告警脚本,虽然不够实用但是还是贴上:

#!/bin/sh
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin:$HOME/bin:/usr/local/bin:/usr/bin
export PATH
export LANG=en_US.UTF-8

obj="$1"
tt="$2"
mesg="$3"
nohup curl -d "text=${tt}&desp=${mesg}" http://sc.ftqq.com/${obj}.send >/dev/null 2>&1 &
赞(0)
未经允许不得转载:主机推广 » 云服务商禁止25端口该如何发送邮件