0

I'm working with email content that has been formatted in html. What I have found is that email addresses are often formatted similarly to html tags. Is there a way to selectively escape strings in html code, but render others as-is?

For example, email addresses are often formatted as "Guy, Some <[email protected]>" How can I escape this, which python sees as an html tag, but leave <br></br><p></p>, etc. intact and render them?

edited

I'm dealing with raw emails that have been preserved as text. In the following example, I want all of the html tags to render normally. However, it also tries to render email addresses that are stored in the following format [email protected] and then it gives me an error. So my challenge has been to find all of the email addresses, but leave the html tags alone.

<p>From: Guy, Some <[email protected]></p><br>
<br>
<p>Sent: Friday, January 21, 2022 2:16 PM</p><br>
<br>
<p>To: Another Guy <[email protected]></p>
<br>
<p>Subject: Really Important Subject</p>
<br>
<p> <br>Good morning,
<br>This is sample text<br> </p>
<br>
<p>Thanks for all your help!!!
<br>
<p> </p>

1 Answer 1

1

You can use html &lt; and &gt; to make <> inside html document if you're passing this email tags from django then you've to use safe so it will rendered as pure html code like this

"Guy, Some {{email|safe}}"

EDIT

before rendering your html you can extract all emails with <email> for example

import re

data = '''
<p>From: Guy, Some <[email protected]></p><br>
<br>
<p>Sent: Friday, January 21, 2022 2:16 PM</p><br>
<br>
<p>To: Another Guy <[email protected]></p>
<br>
<p>Subject: Really Important Subject</p>
<br>
<p> <br>Good morning,
<br>This is sample text<br> </p>
<br>
<p>Thanks for all your help!!!
<br>
<p> </p>
'''
emails_to_parse = re.findall('[A-z]+@[A-z]+.[A-z]+', data) # this will return ['[email protected]', '[email protected]']

emails_to_remove = re.findall('<[A-z]+@[A-z]+.[A-z]+>', data) # this will return ['<[email protected]>', '<[email protected]>']

for i in emails_to_parse:
  for j in emails_to_remove:
    data = data.replace(j,i)
print(data)

above code gives this output

<p>From: Guy, Some [email protected]</p><br>
<br>
<p>Sent: Friday, January 21, 2022 2:16 PM</p><br>
<br>
<p>To: Another Guy [email protected]</p>
<br>
<p>Subject: Really Important Subject</p>
<br>
<p> <br>Good morning,
<br>This is sample text<br> </p>
<br>
<p>Thanks for all your help!!!
<br>
<p> </p>

I'll suggest to look at this post

Sign up to request clarification or add additional context in comments.

1 Comment

Hello @jvanheijzen check I've edited my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.