Python Forum
Python - Hidden Text / Html Mail - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python - Hidden Text / Html Mail (/thread-43972.html)



Python - Hidden Text / Html Mail - python1337 - Feb-07-2025

I have a Python tool that I want to use to send emails.

I want to send a hidden text within an html mail without the plaintext being displayed - Currently my emails end up in spam, so I want to try to stop the emails ending up in spam.

I hope someone can help me, thank you very much.


RE: Python - Hidden Text / Html Mail - python1337 - Feb-08-2025

The whole thing looks something like this :

def send_email(recipient, subject, html_content):
try:
# Replace image URLs in the HTML template so that they point to the content IDs
html_content = html_content.replace('src=“logo.png”', 'src=“cid:logo_img” alt=“company logo”')
html_content = html_content.replace('src=“banner.png”', 'src=“cid:banner_img” alt=“Company banner”')

# Create the outer container as “related” to enable inline images
message = MIMEMultipart(“related”)
message[“From”] = f “Comdirect <{FROM_EMAIL}>”
message[“To”] = recipient
message[“Subject”] = subject

# Create a multipart/alternative container for the plain text and HTML versions
alternative_part = MIMEMultipart(“alternative”)

# Create the plain text content (should reflect the essential content if possible)
plain_text = (
“Dear '\n\n”
“We ask for your understanding and thank you for your help in making our online offer as secure as possible.\n\n”
“With kind regards,\n”
“Thomas Schaufler\n”
)

# First add the plain text and then the HTML part (the HTML part is displayed in HTML-capable clients)
alternative_part.attach(MIMEText(plain_text, “plain”))
alternative_part.attach(MIMEText(html_content, “html”))

# Attach the alternative part to the main container
message.attach(alternative_part)

# Load the inline images (logo.png and banner.png) from the BASE_DIR and attach them as an attachment
logo_path = os.path.join(BASE_DIR, “logo.png”)
banner_path = os.path.join(BASE_DIR, “banner.png”)

# Embed logo
with open(logo_path, “rb”) as f:
logo_data = f.read()
logo_img = MIMEImage(logo_data)
logo_img.add_header(“Content-ID”, “<logo_img>”)
logo_img.add_header(“Content-Disposition”, “inline”, filename=“logo.png”)
message.attach(logo_img)

# Embed banner
with open(banner_path, “rb”) as f:
banner_data = f.read()
banner_img = MIMEImage(banner_data)
banner_img.add_header(“Content-ID”, “<banner_img>”)
banner_img.add_header(“Content-Disposition”, “inline”, filename=“banner.png”)
message.attach(banner_img)

# Establish the connection to the SMTP server, start TLS, login and send the e-mail
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(SMTP_USER, recipient, message.as_string())
print(f “Email successfully sent to {recipient}.”)
except Exception as e:
print(f “Error sending to {recipient}: {e}”)
- how can i include a plain text in my send mail logic so that i can send my mail in html and plain text at the same time via mime?
i have tried it now with several approaches but unfortunately when i receive the message i see that only the html part is loaded and i have 0% text in the email at mail-tester.com