Answers for "how to add a file to an email in python"

21

python copy file

from shutil import copyfile
copyfile(src, dst)
Posted by: Guest on March-02-2020
0

how to add a file to an email in python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders


SUBJECT = "Email Data"

msg = MIMEMultipart()
msg['Subject'] = SUBJECT 
msg['From'] = self.EMAIL_FROM
msg['To'] = ', '.join(self.EMAIL_TO)

part = MIMEBase('application', "octet-stream")
part.set_payload(open("text.txt", "rb").read())
Encoders.encode_base64(part)

part.add_header('Content-Disposition', 'attachment; filename="text.txt"')

msg.attach(part)

server = smtplib.SMTP(self.EMAIL_SERVER)
server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string())
Posted by: Guest on August-22-2020

Code answers related to "how to add a file to an email in python"

Python Answers by Framework

Browse Popular Code Answers by Language