Python Forum
Python - Hidden Text / Html Mail
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - Hidden Text / Html Mail
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy only hidden files and folders with rsync Cannondale 2 2,363 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 1,906 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  color column in mail html df bnadir55 0 1,260 Aug-14-2022, 07:11 AM
Last Post: bnadir55
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 2,457 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  Get text from within h3 html tags Pedroski55 8 6,778 Jan-05-2022, 06:50 AM
Last Post: Larz60+
  reading html and edit chekcbox to html jacklee26 5 4,434 Jul-01-2021, 10:31 AM
Last Post: snippsat
  Mail issue Mihil 3 3,656 Dec-03-2020, 05:25 AM
Last Post: Mihil
  Run hidden exe samuelbachorik 0 3,587 Aug-02-2020, 03:10 PM
Last Post: samuelbachorik
  Making .exe file that requires access to text and html files ClassicalSoul 0 2,022 Apr-23-2020, 05:03 PM
Last Post: ClassicalSoul
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 3,534 Mar-19-2020, 02:46 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020