89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
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 = """\
|
|
<html>
|
|
<body>
|
|
<p>Hello,</p>
|
|
<p>This email contains two embedded images and one regular attachment.</p>
|
|
<p>Here is the first image (CID):</p>
|
|
<img src="cid:image1.jpg" alt="First Image">
|
|
<p>And here is the second one (CID):</p>
|
|
<img src="cid:logo.png" alt="Company Logo">
|
|
<p>Please see the attached PDF for more information.</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# 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', '<image1.jpg>')
|
|
msg_image1.add_header('Content-Disposition', 'inline')
|
|
msg_root.attach(msg_image1)
|
|
|
|
# Attach 'logo.png'
|
|
with open('logo.png', 'rb') as fp:
|
|
msg_logo = MIMEImage(fp.read(), _subtype="png")
|
|
msg_logo.add_header('Content-ID', '<logo.png>')
|
|
msg_logo.add_header('Content-Disposition', 'inline')
|
|
msg_root.attach(msg_logo)
|
|
|
|
# --- 4. Attach the regular attachment ---
|
|
# This part is for the file that should be a standard attachment.
|
|
with open('document.pdf', 'rb') as fp:
|
|
part = MIMEBase('application', 'pdf')
|
|
part.set_payload(fp.read())
|
|
encoders.encode_base64(part)
|
|
part.add_header('Content-Disposition', 'attachment', filename='document.pdf')
|
|
msg_root.attach(part)
|
|
|
|
# --- 5. Send the email using SMTP ---
|
|
try:
|
|
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
|
|
smtp.login(sender_email, sender_password)
|
|
smtp.send_message(msg_root)
|
|
print("Email sent successfully!")
|
|
except Exception as e:
|
|
print(f"Failed to send email: {e}") |