0

I have wrote an app to check stock prices and if there was a significant change pull news articles and email them to me. Everything works great except I want to format part of the email text as bold. I have tried the \033[1mSome Text\033[0m method the **Some Text** method and the <b>Some Text</b> method but none of them seem to work. Here is a simple example of the code that builds the message and sends the email.

message_header = f"\033[1m{company} ({symbol}) changed by {percent_change}% from ${round(prev_close, 2)}" \
                 f" to ${round(most_recent_close, 2)}.\033[0m\n\n" \
                 f"Here is the related news.\n\n"

for item in range(0, len(top_stories) - 1):
    my_message = my_message + f"From: {top_stories[item]['source']['name']}\n" \
                              f"Title: {top_stories[item]['title']}\n" \
                              f"Brief: {top_stories[item]['description']}\n" \
                              f"Url: {top_stories[item]['url']}\n\n"
            
    if my_message == "":
        my_message = message_header + "Unfortunately, there was no related news.\n\n\n"
    else:
        my_message = message_header + my_message + "\n"

    email_message = email_message + my_message

with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
    connection.starttls()
    connection.login(user=MY_EMAIL, password=PASSWORD)
    connection.sendmail(
        from_addr=MY_EMAIL,
        to_addrs=recipients,
        msg=f"Subject:Stock Alerts!\n\n{email_message}\n\nRegards"
    )

I just want to bold the first line of my message_header i.e. ADOBE INC (ADBE) changed by 2.19% from $643.17 to $657.6.

Below is what my actual email output looks like, I am getting the ** ** <b> </b> or [1m [0m instead of bold text. If I print to the console it comes out fine though. Would love if someone could explain what I am missing here.

[1mADOBE INC (ADBE) changed by 2.19% from $643.17 to $657.6. [0m

Here is the related news.

From: Reuters Title: 'December to Forget': Automakers, retailers cut TV ads amid supply chain woes - Reuters

Brief: For years, luxury vehicle brands have promoted holiday season sales with slogans like Lexus's "A December to remember."

Url: https://www.reuters.com/business/december-forget-automakers-retailers-cut-tv-ads-amid-supply-chain-woes-2021-11-11/

furstukin
  • 1
  • 1
  • 1
    Does this answer your question? [Sending HTML email using Python](https://stackoverflow.com/questions/882712/sending-html-email-using-python) – Jonathon Reinhart Nov 14 '21 at 03:46

0 Answers0