2

I've script written in Python3, it's doing couple of things and one of them is sending email. That's the code:

    import smtplib
    import os
    import json
    import re
    from datetime import datetime 
    from smtplib import SMTP
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    def send_mail():
    
        message = MIMEMultipart()
        message['Subject'] = mailtitle
        message['From'] = mailfrom
        message['To'] = mailto
    
        body_content = ("Text message")
    
        message.attach(MIMEText(body_content, 'plain'))
        msg_body = message.as_string()
    
        server = smtplib.SMTP('localhost')
        server.set_debuglevel(1)
        server.sendmail(message['From'], message['To'], msg_body)
        server.quit()

It's working fine on my virtual box environment but when I've put it on real testing environment I'm getting below output:

Traceback (most recent call last):
      File "send_email.py", line 166, in <module>
        send_mail()
      File "send_email.py", line 159, in send_mail
        server = smtplib.SMTP('localhost')
      File "/usr/lib64/python3.6/smtplib.py", line 251, in __init__
        (code, msg) = self.connect(host, port)
      File "/usr/lib64/python3.6/smtplib.py", line 336, in connect
        self.sock = self._get_socket(host, port, self.timeout)
      File "/usr/lib64/python3.6/smtplib.py", line 307, in _get_socket
        self.source_address)
      File "/usr/lib64/python3.6/socket.py", line 724, in create_connection
        raise err
      File "/usr/lib64/python3.6/socket.py", line 713, in create_connection
        sock.connect(sa)
    OSError: [Errno 113] No route to host

Could you pls advise what can cause the issue? Firewall settings? Missing libraries? Many thanks

piooreq
  • 57
  • 1
  • 3

1 Answers1

1

I resolved that issue in the end. The problem was related to the blocked SMTP port 25 on the firewall. When the port has been opened, my issue disappeared.

ouflak
  • 2,458
  • 10
  • 44
  • 49
piooreq
  • 57
  • 1
  • 3