我想发送一封包含 html 代码和图像的电子邮件
但在 gmail 中它说图像已附加,我不想要这样,我只想要电子邮件正文中的图像。
html_content=f"<img src="cid:banner" alt="" style="width:80%;">"
msg = MIMEMultipart('related')
html_part = MIMEText(html_content, 'html')
msg.attach(html_part)
with open(image_path, 'rb') as f:
image_data = f.read()
image_attachment = MIMEImage(image_data,'html', name='Logo')
image_attachment.add_header('Content-ID','<banner>') # Unique identifier for the image
msg.attach(image_attachment)
# Subject of E-mail
msg['Subject'] = f"Aire Frio Maracay ❄"
# Sender E-mail Id
sender = '[email protected]'
msg['From'] = sender
# Receiver E-mail Id
recipient = '[email protected]'
msg['To'] = recipient
# SMTP Server
server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
# SMTP Server Login Credentials
server.login(sender, 'gYDY04USz6ru')
# Gives Send Mail Request to SMTP Server
server.sendmail(sender, recipient, msg.as_string())
# Quit or Logout from SMTP Server
server.quit()
我尝试在youtube中搜索并询问Gemini IA,但我没有找到解决方案
The problem is that you are attaching the image as a separate entity in the email, instead of embedding it directly in the HTML.
Here's how to embed the image directly in the HTML using base64 encoding:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import base64
def send_email_with_image(image_path, recipient_email):
"""Sends an email with an embedded image.
Args:
image_path (str): The path to the image file.
recipient_email (str): The recipient's email address.
"""
# Read the image and encode it in base64
with open(image_path, 'rb') as img_file:
image_data = img_file.read()
encoded_image = base64.b64encode(image_data).decode('utf-8')
# Create the HTML content with the embedded image
html_content = f"""
<html>
<body>
<img src="data:image/png;base64,{encoded_image}" alt="Image" style="width:80%;">
</body>
</html>
"""
# Create the email message
msg = MIMEMultipart('related')
msg['Subject'] = "Aire Frio Maracay ❄"
msg['From'] = '[email protected]'
msg['To'] = recipient_email
# Attach the HTML part
msg.attach(MIMEText(html_content, 'html'))
# Send the email
server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
server.login('[email protected]', 'gYDY04USz6ru') # Replace with your actual password
server.sendmail('[email protected]', recipient_email, msg.as_string())
server.quit()
# Example usage:
image_path = 'path/to/your/image.png' # Replace with your image path
recipient_email = '[email protected]'
send_email_with_image(image_path, recipient_email)
Explanation:
-
Base64 Encoding:
The code reads the image file in binary mode (
'rb'
), converts the binary data to base64 encoding, and then decodes it into a string. -
Embedding in HTML:
The base64 encoded image string is inserted directly into the
src
attribute of the<img>
tag within the HTML content. We use the data URI scheme (data:image/png;base64,
) to indicate that the image data is embedded within the HTML itself. - Sending the Email: The rest of the code remains similar, constructing and sending the email with the updated HTML content.
Now, the image should be displayed directly within the email body without any attachments. Make sure to:
-
Replace
"path/to/your/image.png"
with the actual path to your image file. -
Verify Image Type:
If your image is not a PNG, adjust the
data:image/png;base64,
part to match the correct MIME type (e.g.,data:image/jpeg;base64,
for JPEG images).