0

So im having trouble sending a textfile with my program in python

import smtplib, email, ssl
from email.mime.text import MIMEText


port = 465 #465 is the standard port of SMTP over SSL
smtp_server = "smtp.gmail.com"
sender_email = "xxx@gmail.com"
receiver_email = "xxx@gmail.com"
password = input("Enter your password: ")
filename = "C:\\Users\\nothi\\Desktop\\logs.txt"
message = """\
Subject: Hi there

Attached is the log file from the logging system."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message, message.attach(MIMEText(open(filename).read())))

When i run the code it prompts me for my password which is file but after i get the following error:

Traceback (most recent call last):
  File "C:/Users/nothi/Desktop/code/python/emailfiletest.py", line 19, in <module>
    server.sendmail(sender_email, receiver_email, message, message.attach(MIMEText(open(filename).read())))
AttributeError: 'str' object has no attribute 'attach'
  • Check out - https://stackoverflow.com/questions/26311030/attributeerror-str-object-has-no-attribute-policy – frankr6591 Nov 24 '20 at 00:30
  • You need to create an email message object and attach attachments to it as demonstrated in the [examples](https://docs.python.org/3/library/email.examples.html) in the docs. – snakecharmerb Nov 25 '20 at 09:01

0 Answers0