-1

To send an email, I use a certain function:

def send_email(self, subject, to_addr, from_addr, content):
    body_text = ''
    for cnt in content:
        body_text += str(cnt)
    BODY = '\r\n'.join((
        'From: %s' % from_addr,
        'To: %s' % to_addr,
        'Subject: %s' % subject,'', body_text
    ))

    server = smtplib.SMTP(self.host)
    server.sendmail(from_addr, to_addr, BODY.encode('utf-8'))
    server.quit()

As you can see, the message body is in the body_text variable.

The message is generated in this piece of code:

class Event():
    def __init__(self, path):
        self.path = path
        self.time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    def __str__(self):

        return ('Time: ' + self.time + '\tPath: '+ '"' + self.path + '"' + '\n')

Could you tell me please, how can I format a message sent by mail?

I would like that it was possible to make the text bold, italic or underlined, insert a hyperlink and, accordingly, all this would be displayed correctly in the letter itself.

Now if I use the class color in the function where the text is formed:

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'


def __str__(self):

    return (color.BOLD + 'Time: ' + color.END + self.time + '\tPath: '+ '"' + self.path + '"' + '\n')

then the following characters will be displayed in the incoming message:

enter image description here

After changes, if you do print(message_str):

enter image description here

Alex Rebell
  • 465
  • 3
  • 15
  • You could format it to your desire using HTML. – binaryescape Aug 05 '23 at 12:56
  • ASCII escape codes only work if the program that's rendering the text decides to interpret them. Virtually no email clients (or any other non-terminal emulator programs) will attempt to render ASCII escape codes. – Brian61354270 Aug 05 '23 at 13:00

2 Answers2

1

You can add HTML formatting to the email content, you can change the send_email function as follows:

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

def send_email(self, subject, to_addr, from_addr, content):
    # Create a multipart message
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject

    # Create HTML content
    body_html = '<html><body>'
    for cnt in content:
        body_html += '<p>' + str(cnt) + '</p>'
    body_html += '</body></html>'

    # Attach HTML content to the message
    msg.attach(MIMEText(body_html, 'html'))

    # Convert the message to a string
    message_str = msg.as_string()

    # Send the email
    server = smtplib.SMTP(self.host)
    server.sendmail(from_addr, to_addr, message_str.encode('utf-8'))
    server.quit()

Study the email documentation: https://docs.python.org/3/library/email.examples.html

zcxw
  • 5
  • 1
  • Thank you very much for your answer. I applied your code, but unfortunately the letter comes empty, i.e. there is no content in it. – Alex Rebell Aug 06 '23 at 07:06
  • I edited my poros, pointing out the problem there – Alex Rebell Aug 06 '23 at 07:17
  • Additionally, I would advise you to use a library such as [jinja](https://jinja.palletsprojects.com/en/3.1.x/) to fill in the gaps and generate the HTML from a template. Many email clients only support a subset of CSS, thus you're required to work-around this with some quit verbose HTML. – Julian Kirsch Aug 08 '23 at 16:33
0

In general, it turned out, here is my code:

def send_email(self, subject, to_addr, from_addr, content):
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject
    body_html = '<html><body>'
    for cnt in content:
        body_html += ('<html><head></head><body><p><b>Time: </b>' + '<em>' + str(cnt.getTime()) + '</em>'
                     + '<br><b>Path: </b>' + 
                      '<a href='+'"'+ str(cnt.getPath()) + '">' + str(cnt.getPath()) + ''</a></body></html>'')

    part_msg = MIMEText(body_html, 'html')
    msg.attach(part_msg)
    server = smtplib.SMTP(self.host)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.quit()
Alex Rebell
  • 465
  • 3
  • 15