Score:0

Unable to connect to SMTP - "smtp.outlook.com",587 using Python2.7 from Linux server

in flag

I have a small python script below which sends emails to my company outlook email. it works fine on my personal computer but when i am deploying it to server it keeps running and timeout but doesn't send email .

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()

Below is the error when it gets timedout-

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

I am not able to do telnet to outlook, it also gets timed out. see below:-

[linux_server Python]$ telnet smtp.outlook.com 587
Trying 40.101.4.2...

^C
[linux_server Python]$

below are listening ports on this server.

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]$

Can someone help me understand the issue and fix it. Or is there any alternate way to config this email sending script. Any help is appreciated . Thanks in advance !

vidarlo avatar
ar flag
*Where* are you deploying it? It looks like port 587 is blocked outgoing from where you are.
Dharmendra Kumar avatar
in flag
@vidarlo yes, seems so. Any other alternative ways ?
vidarlo avatar
ar flag
Talk with the provider to see what they suggest.
Dharmendra Kumar avatar
in flag
@vidarlo provider means the server owner ?
vidarlo avatar
ar flag
yes, that's right
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.