from email.mime.text import MIMEText msg = MIMEText("Hello, this is a plain text message")
from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart msg = MIMEMultipart() msg.attach(MIMEText("Hello, this is an HTML message
", 'html'))
import os from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders msg = MIMEMultipart() attach_file = open('file.txt', 'rb') payload = MIMEBase('application', 'octate-stream') payload.set_payload((attach_file).read()) encoders.encode_base64(payload) payload.add_header('Content-Disposition', 'attachment', filename='file.txt') msg.attach(payload)In the above examples, we have used the email.MIMEMultipart package to create different types of messages with text, HTML and attachments. We have also used other package libraries such as email.mime.text, email.mime.base and email.encoders to enable and attach different types of data into the MIME objects. The as_string() method is used to print/send the message as a string for final use.