之前在Red Hat Enterprise Linux Server release 6.1 (Santiago)上使用Python发邮件正常,迁移到CentOS就不行了,同样的代码,很是郁闷,见域名:https://blog.csdn.net/zhu6201976/article/details/85809239
经过数小时的折腾,查了大量资料,迎来的却是一次次失败,几乎进入到崩溃的边缘......
终于看到一遍文章,解决了该问题:https://blog.csdn.net/ithongchou/article/details/83578337
import smtplib
from email.header import Header
from email.mime.text import MIMEText
def main():
# 方法1:第三方 SMTP 服务,在Centos7测试正常
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "[email protected]" # 用户名
mail_pass = "******" # 使用自己的口令
sender = '[email protected]'
receivers = ['[email protected]', '[email protected]'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
message = MIMEText('前5分钟总访问量:%d,服务器异常量:%d' % (0, 0), 'plain', 'utf-8')
message['From'] = Header("47.106.66.177", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'ads_nginx.log预警'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP_SSL(timeout=10)
smtpObj.connect(mail_host, 465)
smtpObj.login(mail_user, mail_pass)
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
if __name__ == '__main__':
main()