Send mail to an IP Address using python
Question: So I am trying to send mails via a python script. It is working fine using the
usual format of the receiver address "user@domain.tld". When I'm now trying to
use the script with a receiver "user@[IP-Address] all my debug output looks
good and the sendmail method works, but the mail is never received. I get the
IP address via dig from my terminal.

This is my method expecting a receiver as parameter (cut some unimportant
stuff out and obfuscated the real addresses/credentials)

    
    
    def sendmail(receiver):
        msg = MIMEText('This is the body of the mail.')
        msg['From'] = email.utils.formataddr(('me', myaddr))
        msg['To'] = email.utils.formataddr(('me', receiver))
        msg['Subject'] = "Python Mail Script"
    
    
        server = smtplib.SMTP(smtpServer, smtpPort)
        try:
            server.set_debuglevel(True)
    
            # identify ourselves, prompting server for supported features
            server.ehlo()
    
            # If we can encrypt this session, do it
            if server.has_extn('STARTTLS'):
                server.starttls()
                server.ehlo() # re-identify ourselves over TLS connection
    
            server.login("me@...", "...")
            server.sendmail("me@...", toAddr,  msg.as_string())
        finally:
            server.quit()
    

The output I get for sending a message to an IP-Address is (again I obfuscated
the smtp server and mail/ip addresses):

    
    
    send: 'ehlo [127.0.1.1]\r\n'
    reply: '250-mail.example.de\r\n'
    reply: '250-PIPELINING\r\n'
    reply: '250-SIZE 102400000\r\n'
    reply: '250-VRFY\r\n'
    reply: '250-ETRN\r\n'
    reply: '250-STARTTLS\r\n'
    reply: '250-AUTH PLAIN LOGIN\r\n'
    reply: '250-AUTH=PLAIN LOGIN\r\n'
    reply: '250-ENHANCEDSTATUSCODES\r\n'
    reply: '250-8BITMIME\r\n'
    reply: '250 DSN\r\n'
    reply: retcode (250); Msg: mail.example.de
    PIPELINING
    SIZE 102400000
    VRFY
    ETRN
    STARTTLS
    AUTH PLAIN LOGIN
    AUTH=PLAIN LOGIN
    ENHANCEDSTATUSCODES
    8BITMIME
    DSN
    send: 'STARTTLS\r\n'
    reply: '220 2.0.0 Ready to start TLS\r\n'
    reply: retcode (220); Msg: 2.0.0 Ready to start TLS
    send: 'ehlo [127.0.1.1]\r\n'
    reply: '250-mail.example.de\r\n'
    reply: '250-PIPELINING\r\n'
    reply: '250-SIZE 102400000\r\n'
    reply: '250-VRFY\r\n'
    reply: '250-ETRN\r\n'
    reply: '250-AUTH PLAIN LOGIN\r\n'
    reply: '250-AUTH=PLAIN LOGIN\r\n'
    reply: '250-ENHANCEDSTATUSCODES\r\n'
    reply: '250-8BITMIME\r\n'
    reply: '250 DSN\r\n'
    reply: retcode (250); Msg: mail.example.de
    PIPELINING
    SIZE 102400000
    VRFY
    ETRN
    AUTH PLAIN LOGIN
    AUTH=PLAIN LOGIN
    ENHANCEDSTATUSCODES
    8BITMIME
    DSN
    send: 'AUTH PLAIN AHRob21hc0B0b3Jh6feldi5kZQBiYWdpbmVy\r\n'
    reply: '235 2.7.0 Authentication successful\r\n'
    reply: retcode (235); Msg: 2.7.0 Authentication successful
    send: 'mail FROM:<me@example.de> size=234\r\n'
    reply: '250 2.1.0 Ok\r\n'
    reply: retcode (250); Msg: 2.1.0 Ok
    send: 'rcpt TO:<me@[IP-ADDRESS]>\r\n'
    reply: '250 2.1.5 Ok\r\n'
    reply: retcode (250); Msg: 2.1.5 Ok
    send: 'data\r\n'
    reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
    reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
    data: (354, 'End data with <CR><LF>.<CR><LF>')
    send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nFrom: me <me@example.de>\r\nTo: me <me@[IP-ADDRESS]>\r\nSubject: Python Mail Script\r\n\r\nThis is the body of the mail.\r\n.\r\n'
    reply: '250 2.0.0 Ok: queued as 4C5A78560196\r\n'
    reply: retcode (250); Msg: 2.0.0 Ok: queued as 4C5A78560196
    data: (250, '2.0.0 Ok: queued as 4C5A78560196')
    send: 'quit\r\n'
    reply: '221 2.0.0 Bye\r\n'
    reply: retcode (221); Msg: 2.0.0 Bye
    

Anyone seeing any mistake or error?

Edit: Using an own smtp postfix server in the university(unfortunatly no
access to the outside world) and sending the mail to user@[IP-ADDRESS] of this
very server the mail arrives. Probably another sign that in the problematic
case stated above the smtp-server of the receiver just doesn't allow an IP
address as destination.

Edit2: /var/log/mail.log

    
    
    mail.log:Sep  9 00:34:06 mail postfix/qmgr[4854]: A30168560199: from=<me@example.de>, size=1197, nrcpt=1 (queue active)
    
    mail.log:Sep  9 00:34:06 mail postfix/smtp[19355]: A30168560199: to=<me@[IP-ADDRESS]>, relay=none, delay=314, delays=313/0.04/0.01/0, dsn=4.4.1, status=deferred (connect to IP-ADDRESS[IP-ADDRESS]:25: Connection refused)
    
    mail.log:Sep  9 00:38:31 mail postfix/smtpd[19907]: warning: Illegal address syntax from my_host_where_the_python_script_runs[IP of me] in RCPT command: <me@IP-ADDRESS>
    

So the connection from the SMTP server I am using to the recipient server is
refused and 4 minutes later the smtp deamon says it received a wrong syntax
even though in the handshake where I am sending via my script it say 250 Ok
... So basically I think the recipient SMTP refuses it. Thx guys


Answer: Domain names in email addresses are much like domain names for web sites - a
single server at a single IP address is often responsible for many different
domain names, and so in those cases you must supply the name in your request.
For example, a mail server at `1.2.3.4` may service both `abc.com` and
`xyz.com`. If you say `RCPT TO: <joe@[1.2.3.4]>`, the server has no idea if
you are trying to reach `joe@abc.com` or `joe@xyz.com`. The server might
arbitrarily pick one (and guess wrong), accept the message and drop it, or
reject the message.

Unless you have somehow verified with the server operator that the server will
deliver `user@[ip]`-addressed messages to the domain you expect, you should
not assume that it will.

