SendMail Freezes infinitely

def send_mail(global_config, result, args, rmm_version):
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    import smtplib
    from email.mime.base import MIMEBase
    from email import encoders
    import io
    msg = MIMEText(msg_body, 'HTML')
    msg['Subject'] = subject
    msg['From'] = me = global_config.get('smtp','from')
    recipients = global_config.get('smtp','to').split(',')
    msg['To'] = ", ".join(recipients)
    smtpObj = smtplib.SMTP(global_config.get('smtp','server'), 8084)
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.ehlo()
    smtpObj.login(global_config.get('smtp','login_user'),global_config.get('smtp','login_password'))
    smtpObj.ehlo()
    smtpObj.sendmail(me, recipients, msg.as_string())
    smtpObj.quit()
    return result

I tried the above script to send mail but it is getting freezed at the very first step where we call smtplib.SMTP() function.
What is the issue in the code because of which it is freezing. Also ping to the server is working but telnet to the server to port 8084 is failing.
it was working for python2.6 version but freezes for python3.9 version.

Hi

I am not familiar with these library packages. However, from a practical standpoint, here are a few observations that I noticed:

  1. The following imports are not being used inside your function:
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email import encoders
    import io

If they are not being used, don’t import them if there is no need.

  1. There appears to be two parameters in your function header that are not being used within the function body: args and rmm_version. If they are not needed, then why include them? If you are expecting a list of values for the parameter args, it should be defined as *args, with an asterisk (*). The last parameter should be defined as arbitrary keyword arguments, i.e., **kwargs. If you are not expecting keyword arguments, then simply place it before *args.

  2. The result parameter in the function header is the same variable being returned by the function (return result). This in effect nullifies the entire function body because it is not being processed or redefined (though a different / unique name is preferred). The following figure highlights what is essentially happening.

    bypassing_function_body

You might want to review these key points.