March 2019
Beginner
490 pages
12h 40m
English
To send messages with attachments, you must create an object instance of the MimeMultipart() class. If there are multiple attachments, these can be built sequentially. In this example, we are attaching two text files to the message:
from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextmessage = MIMEMultipart()message['From'] = Header("Sender", 'utf-8')message['To'] = Header("Receiver", 'utf-8')message['Subject'] = Header('Python SMTP', 'utf-8')message.attach(MIMEText('Python SMTP', 'plain', 'utf-8'))file1 = MIMEText(open('file1.txt', 'rb').read(), 'base64', 'utf-8')file1["Content-Type"] = 'application/octet-stream'file1["Content-Disposition"] = 'attachment; filename="file1.txt"' ...Read now
Unlock full access