Attach a txt file in Python smtplib

The same way, using msg.attach:

from email.mime.text import MIMEText

filename = "text.txt"
f = file(filename)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename=filename)           
msg.attach(attachment)

Leave a Comment