我有下面的一个小的 python 脚本,它可以将电子邮件发送到我公司的 Outlook 电子邮件。它在我的个人电脑上运行良好,但是当我将它部署到服务器时,它继续运行并超时,但不会发送电子邮件。
import os
import smtplib
import subprocess
from sys import exit
cmd = 'df -kh'
p1 = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
std,err = p1.communicate()
output=std.decode()
#print (output)
smtp=smtplib.SMTP('smtp.outlook.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('email','password')
subject = 'Send_Disk_Info_Daily'
body = output;
msg = (("subject : {} ".format(subject))+("\n\n{}".format(body)))
smtp.sendmail('email', 'email',msg)
smtp.quit()
以下是超时时的错误-
Traceback (most recent call last):
File "send_disk_info.py", line 16, in <module>
smtp=smtplib.SMTP_SSL('smtp.outlook.com', 587, timeout=20)
File "/usr/lib64/python2.7/smtplib.py", line 789, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout)
File "/usr/lib64/python2.7/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.7/smtplib.py", line 315, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.7/smtplib.py", line 794, in _get_socket
new_socket = socket.create_connection((host, port), timeout)
File "/usr/lib64/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable
我无法通过 telnet 连接到 outlook,它也超时了。见下文:-
[linux_server Python]$ telnet smtp.outlook.com 587
Trying 40.101.4.2...
^C
[linux_server Python]$
以下是该服务器上的监听端口。
linux_server Python]$ netstat -tulpn|grep -i listen
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
tcp6 0 0 :::5666 :::* LISTEN -
tcp6 0 0 :::37712 :::* LISTEN 45077/java
tcp6 0 0 :::8080 :::* LISTEN 44458/java
tcp6 0 0 :::8085 :::* LISTEN 44458/java
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:25 :::* LISTEN -
tcp6 0 0 :::32858 :::* LISTEN 44648/java
[linux_server Python]$
有人能帮我理解这个问题并修复它吗?或者还有其他方法可以配置这个电子邮件发送脚本吗?任何帮助我都感激不尽。提前致谢!