Email.mime.multipart
A Python module!
email.mime.multipart
is a class in the email.mime
module, which is part of the Python Standard Library. It's used to create a MIME multipart message, which is a type of email message that contains multiple parts, such as text, images, and attachments.
Here's a brief overview of the class:
Class: email.mime.multipart.MIMEMultipart
Description: A class for creating a MIME multipart message.
Attributes:
subtype
: The subtype of the multipart message (e.g., "alternative", "related", "mixed").boundary
: The boundary string used to separate the parts of the message.
Methods:
add_header(name, value)
: Adds a header to the message.attach(filename, maintype, subtype)
: Attaches a file to the message.append(part)
: Appends a part to the message.as_string()
: Returns the message as a string.
Example usage:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
msg = MIMEMultipart()
msg['Subject'] = 'Test message'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
text_part = MIMEText('Hello, world!')
msg.attach(text_part)
image_part = MIMEImage(open('image.jpg', 'rb'))
msg.attach(image_part)
print(msg.as_string())
This example creates a MIME multipart message with two parts: a text part and an image part. The attach()
method is used to add each part to the message, and the as_string()
method is used to convert the message to a string.