Python smtplib sometimes fails sending
Question: I wrote a simple "POP3S to Secure SMTP over TLS" MRA script in Python (see
below).

It works fine, but sometimes it returns "Connection unexpectedly closed" while
trying to send via SMTP. Running the script again will deliver that message
successfully.

Please give me some suggestions why it would fail to deliver a message
sometimes but at the next run it delivers exactly this message successfully!?

    
    
    #! /usr/bin/env python
    
    import poplib
    import email
    
    def forward_pop3_smtp( smtp_credentials, pop3_credentials, forward_address):
        pop3_server = pop3_credentials[0]
        pop3_port = pop3_credentials[1]
        pop3_user = pop3_credentials[2]
        pop3_password = pop3_credentials[3]
    
        message_recipient = forward_address
    
        server = poplib.POP3_SSL( pop3_server, pop3_port)
        server.user( pop3_user)
        server.pass_( pop3_password)
    
        for messages_iterator in range( len( server.list()[1])):
            message_list = server.retr( messages_iterator + 1)[1]
    
            message_string = ''
            for message_line in message_list:
                message_string += message_line + '\n'
    
            message_message = email.message_from_string( message_string)
            message_message_as_string = message_message.as_string()
            message_sender = message_message[ 'From']
            print( 'message_sender = ' + message_sender)
    
            smtp_return = send_smtp( smtp_credentials, message_sender, message_recipient, message_message_as_string)
            print( 'smtp_return = ' + str(smtp_return))
            if smtp_return == 0:
                print( 'Deleting message ' + message_message[ 'Subject'] + ':\n')
                return_delete = server.dele( messages_iterator + 1)
                print( 'return_delete = \n' + str(return_delete))
                print( '\n')
        server.quit()
    
    def send_smtp( smtp_credentials, message_sender, message_recipient, message_message_as_string):
        smtp_server = smtp_credentials[0]
        smtp_port = smtp_credentials[1]
        smtp_user = smtp_credentials[2]
        smtp_password = smtp_credentials[3]
    
        import smtplib
        exception = 0
        try:
            server = smtplib.SMTP( smtp_server)
            server.starttls()
            server.login( smtp_user, smtp_password)
            smtp_sendmail_return = server.sendmail( message_sender, message_recipient, message_message_as_string)
            server.quit()
        except Exception, e:
            exception = 'SMTP Exception:\n' + str( e) + '\n' + str( smtp_sendmail_return)
        return exception
    
    if __name__ == '__main_':
        print( 'This module needs to be imported!\n')
        quit()
    


Answer: Use Port 587 for TLS. I don't see the script use smtp_port

Use like,

    
    
    server = smtplib.SMTP( smtp_server, int(smtp_port)
    

For Secure SMTP (SMTP + SSL), use smtplib.SMTP_SSL

