Search code examples
pythonsmtpsmtplib

Python Email TypeError: expected string or buffer


In a very simple example of sending an email:

import smtplib
from email.mime.text import MIMEText

message = MIMEText("test message")
s = smtplib.SMTP('localhost')
s.sendmail("example@example.com", ["example@example.com"], message)

I get the following error from the last line of my code:

TypeError: expected string or buffer

What am I doing wrong?


Solution

  • You need to pass message.as_string() to sendmail.

    In the example give, it would look like this:

    s.sendmail("example@example.com", ["example@example.com"], message.as_string())