-1

I am trying to send an email with Python. But I'm having a bug. This is my code:

import smtplib


def sendmail(subject, body):
    smtp = smtplib.SMTP("smtp.gmail.com", 587)
    smtp.ehlo()
    smtp.starttls()
    smtp.login("....@gmail.com", PASSWORD)
    message_body = f"Subject:{subject}\n\n{body}"
    smtp.sendmail("...@gmail.com", "....@gmail.com", message_body)
    smtp.quit()

sendmail("test subject", "test body")

I however get the below bug: AttributeError: 'int' object has no attribute 'encode'

Ololade
  • 99
  • 7

2 Answers2

0

Try this :

smtp.sendmail(sender, recipient, message_body.as_string())
Nishant Agarwal
  • 430
  • 5
  • 17
0

from documentation

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.

  smtp.sendmail(sender, recipient, str(message_body))
sahasrara62
  • 10,069
  • 3
  • 29
  • 44