python smtplib在linux上发送邮件失败解决方式
通过zabbix配置邮件告警的时候,发现邮件访问不了
之后将脚本copy到window上执行发现可以正常发送
linux却不行,以为是python版本问题,经过检验并不是
新建send.py文件 代码如下
import smtplib from email.mime.text import MIMEText import sys # configure your own parameters here #下面邮件地址的smtp地址 mail_host = 'smtp.163.com' #用来发邮件的邮箱,在发件人抬头显示(不然你的邮件会被当成是垃圾邮件) mail_user = 'chy1559843332@163.com' # 客户端授权码 mail_auth = '********' # 发送方显示的名称 send_name = mail_user # 接收方显示的名称 recv_name = mail_user def excute(to, title, content): msg = MIMEText(content, 'plain', 'utf-8') msg['From'] = send_name msg['To'] = recv_name msg['Subject'] = title server = smtplib.SMTP(mail_host, 25) server.login(mail_user,mail_auth) server.sendmail(mail_user,to,msg.as_string()) server.quit() if __name__ == '__main__': excute('1559843332@qq.com', 'chyhis is title', 'this is content')
在windows下,通过python send.py 执行之后即可收到邮件
之后放在linux上却 /usr/local/python3/lib/python3.7/socket.py
Traceback (most recent call last): File "aa.py", line 22, in <module> excute('1559843332@qq.com', 'chy this is title', 'mcdh cnhk') File "aa.py", line 16, in excute server = smtplib.SMTP(mail_host, 465) File "/usr/local/python3/lib/python3.7/smtplib.py", line 251, in __init__ (code, msg) = self.connect(host, port) File "/usr/local/python3/lib/python3.7/smtplib.py", line 338, in connect (code, msg) = self.getreply() File "/usr/local/python3/lib/python3.7/smtplib.py", line 387, in getreply line = self.file.readline(_MAXLINE + 1) File "/usr/local/python3/lib/python3.7/socket.py", line 589, in readinto return self._sock.recv_into(b)
之后参考https://www.jianshu.com/p/fc55404b6db7
解决方式:
smtplib.SMTP('smtp.163.com', 25)
修改为
smtplib.SMTP_SSL('smtp.163.com', 465)
之后在linux上执行python send.py,嗯,完美!
面朝大海```春暖花开