Send a formatted email without html

Viewed 289

I have a problem. I want to send a formatted email without html. Is there an option to send a formatted email with Python without html ?

For example, on GMail, if I want to write a message, I can format it within GMail. But it is not displayed as HTML. The problem is that with HTML, often the messages are not displayed directly, unlike when I format them directly.

You can format your mail e.g. enter image description here

What I want:


enter image description here


import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "your@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

I tried

class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'

print(color.BOLD + 'Hello World !' + color.END)

[E-Mail Output]
[1m Hello World [0mwie
4 Answers

The formatting GMail is applying is actually HTML under the hood. For example, if you bold some text, that text is actually being wrapped in a <b></b> tag. Instead of displaying the raw HTML, GMail's compose email window is using that HTML to render the formatting, in much the way that a browser will render HTML instead of displaying raw HTML.

The Python interpreter itself doesn't have a way to render HTML in this way. Some other software would be needed, such as an IDE or text editor, that could generate HTML tags from a user-friendly UI. This generated HTML could then be copy-pasted into your python file.

One way to generate this HTML would be to compose your email in GMail, open the inspector (Ctrl+Shift+I), use the selector to highlight the body of the compose email window, right-click on the parent element, and then select Copy > Inner HTML from the right-click context menu. (This flow is based on Firefox; other browsers may vary)

Rich-Text Format (RTF) is another option. Microsoft Outlook has an option to compose a message in RTF, which may be saved as a file with extension .rtf. The format uses a markup code that employs '{' and '}' style brackets, and unique formatting codes, and not HTML and CSS. Here is a link to a resource that discusses the syntax: Basics of Rich Text Format (RTF)

I expect Python has access to RTF edit controls (i.e., text boxes for placement on a form); creating a message, then copying it to the RTF control, and setting the various properties you are seeking through the control, may be a fast way to generate the syntax.

You could use win32 library and it should look like this.

import win32com.client as win32

olApp =win32.Dispatch("Outlook.Application")
olNS=olApp.GetNameSpace("MAPI")
mailItem= olApp.CreateItem(0)
mailItem.Subject="Hello my friend2"
mailItem.BodyFormat=1
mailItem.Body="Hellooooo"
mailItem.To="emailaddress@email.com"
mailItem._oleobj_.Invoke(*(64209,0,8,0,olNS.Accounts.Item("emailaddress@email.com")))
mailItem.Display()
mailItem.Save()
mailItem.Send()

You can format your email for HTML by using messageObject.subtype == 'html' It will send your email as you formatted it in the first place.

For more information, go to the end of this page. And also check the sixth example from this official page

Related