'Body of my text file has my filename instead of what I'm writing to it when sending email attachment
Going to preface this by saying I'm a complete idiot, so excuse my ignorance. I'm expecting my file to have the content in the body "hey", but instead my file is getting written with the name of my file. Here's the documentation for the EmailMessage for Python which I was following.
Most of the examples for using the EmailMessage package were for files which were already created locally. Could it be that maybe it's not possible to write and send a file at the same time?
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['from'] = "[email protected]"
msg['To']="[email protected]"
msg['Subject']="This is TEST"
s = smtplib.SMTP(host='blah_blah_server.net', port=100)
file_name = "test.txt"
with open(file_name, 'w+') as f:
file_data = f.read()
f.write('hey')
encoded_file = file_data.encode('utf-8')
msg.add_attachment(encoded_file, maintype='text', subtype='plain', filename=file_name )
s.send_message(msg)
# returns a text file with the the content in the body being "test.txt" instead of "hey".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|