Python - Hidden Text / Html Mail

python1337

New member
Local time
Today, 20:11
Joined
Feb 7, 2025
Messages
3
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.
 
Define 'hidden'. You can put it in a span with the text color same as the background. You could set the . visibility set to hidden. There's an input type that is hidden.

Even with all those options it doesn't ensure the mail doesn't go to spam. That could be based on a variety of things
 
We need a mail with HTML and text mail in one, so if the browser can not display HTML or the mail client should display the text part
 
If your email is ending up in "Spam", why is the recipient client putting it there? Is this unsolicited email? Is your sending address a known spammer? Are you using a subject that would cause the recipient to kick it to spam?

And as important, why would you want to hide something in an email? If the recipient actually wants it, you don't need to hide it.
 
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
 
sorry, but you have to go to python forums to really get a better answer.
this forum is about VBA, Ms Access/Excel, etc.
 

Users who are viewing this thread

Back
Top Bottom