Python - Hidden Text / Html Mail

Joined
Feb 7, 2025
Messages
2
Reaction score
0
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.
 
Joined
Feb 7, 2025
Messages
1
Reaction score
0
Here are a few methods to hide text:

1. Using display:none:
Python:
<div style="display: none;">This text is hidden from view.</div>

2. Using visibility: hidden:
Python:
<div style="visibility: hidden;">This text is also hidden.</div>

3. Using small font size and same color as background:
Python:
<span style="font-size: 1px; color: #ffffff;">Hidden text</span>
 
Joined
Feb 7, 2025
Messages
2
Reaction score
0
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
 
Joined
Jul 4, 2023
Messages
583
Reaction score
78
BTW,
Python:
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”
)
to (improved readability)
Python:
plain_text = """Dear,

We ask for your understanding and thank you for your help in making our online offer as secure as possible.

With kind regards,
Thomas Schaufler
"""
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,300
Messages
2,571,537
Members
48,327
Latest member
GWEDalene9
Top