Leo van den Brandt
Leo van den Brandt

Reputation: 11

smtplib.SMTPResponseException with Python Email

I'm trying to send an email with python. Everything works fine except the sending part. Here is my code plus the output. As you can see everything runs well until the sendmail function.

import smtplib
from email.mime.text import MIMEText

Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}\n\n{Body}"
msg = MIMEText(Message)

msg['From'] = "[email protected]"
msg['To'] = ["[email protected]"]

with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls() 
    smtp.ehlo()
    smtp.esmtp_features['auth'] = 'LOGIN DIGEST-MD5 PLAIN'



    smtp.login("[email protected]", ".......")
    print("d")
    smtp.sendmail(msg['From'], msg['To'], msg)
    print("i")
    smtp.close()``
    d
Traceback (most recent call last):
  File "mailtest.py", line 22, in <module>
    smtp.sendmail(msg['From'], msg['To'], msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 886, in sendmail
    (code, resp) = self.data(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 568, in data
    q = _quote_periods(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 176, in _quote_periods
    return re.sub(br'(?m)^\.', b'..', bindata)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/re.py", line 208, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mailtest.py", line 24, in <module>
    smtp.close()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 282, in __exit__
    raise SMTPResponseException(code, message)
smtplib.SMTPResponseException: (421, b'gmx.com Service closing transmission channel - command timeout')

Upvotes: 0

Views: 1827

Answers (1)

chuckx
chuckx

Reputation: 6937

Use smtplib.send_message() instead of smtplib.sendmail().

You're receiving a TypeError because you're providing an email.message.Message object where the method expects a string.

From the documentation for send_message:

This is a convenience method for calling sendmail() with the message represented by an email.message.Message object. The arguments have the same meaning as for sendmail(), except that msg is a Message object.

For comparison, here's the documentation of the arguments for snmtplib.sendmail():

Send mail. The required arguments are an RFC 822 from-address string, a list of RFC 822 to-address strings (a bare string will be treated as a list with 1 address), and a message string.

(...)

msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.

Upvotes: 2

Related Questions