import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.base import MIMEBase from email import encoders # --- Email Configuration --- # Replace with your actual email and app password sender_password = "qrtq wfuj hwpp fhqr" recipient_email =["vikasnale@gmail.com", "vikas@marcoaiot.com", "umeshvdesai@outlook.com"] sender_email = "marcoioitsoft@gmail.com" receiver_emails = ["vikasnale@gmail.com", "vikas@marcoaiot.com"] password = "qrtq wfuj hwpp fhqr" # Use Gmail App Password here # --- 1. Create the top-level container (multipart/related) --- # This container will hold the HTML body and the embedded images. msg_root = MIMEMultipart('related') msg_root['Subject'] = 'Python Email with Embedded Images and Attachment' msg_root['From'] = sender_email msg_root['To'] = recipient_email msg_root.preamble = 'This is a multi-part message in MIME format.' # --- 2. Create the alternative container (multipart/alternative) --- # This part holds both the plain-text and HTML versions of the email content. msg_alternative = MIMEMultipart('alternative') msg_root.attach(msg_alternative) # --- Define the plain text and HTML content --- plain_text_content = """\ Hello, This is the plain text version of the email. The images are not visible here. Please view this email in a client that supports HTML to see the embedded images. """ html_content = """\
Hello,
This email contains two embedded images and one regular attachment.
Here is the first image (CID):
And here is the second one (CID):
Please see the attached PDF for more information.
""" # Attach the plain text and HTML versions to the alternative container msg_alternative.attach(MIMEText(plain_text_content, 'plain')) msg_alternative.attach(MIMEText(html_content, 'html')) # --- 3. Attach the embedded images with Content-ID (CID) --- # The Content-ID is crucial for the HTML body to reference the image. # The 'inline' disposition makes sure the image is displayed within the email body. # Attach 'image1.jpg' with open('image1.jpg', 'rb') as fp: msg_image1 = MIMEImage(fp.read(), _subtype="jpeg") msg_image1.add_header('Content-ID', '