22

I'm sending email through below code:

msg = MIMEText(u'<a href="www.google.com">abc</a>')
msg['Subject'] = 'subject'
msg['From'] = 'xxx'
msg['To'] = 'xxx'

s = smtplib.SMTP(xxx, 25)
s.sendmail(xxx, xxx, msg.as_string())

what I want to receive is

abc

what I actually received is:

<a href="www.google.com">abc</a>
Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63
  • https://docs.python.org/2/library/email-examples.html What you're attempting to do needs to be sent via MIMEText(html,blah) – FirebladeDan Jul 30 '15 at 04:27

2 Answers2

18

You should specify 'html' as the subtype -

msg = MIMEText(u'<a href="www.google.com">abc</a>','html')

Without specifying the subtype separately , the subtype defaults to 'plain' (plain-text). From documentations -

class email.mime.text.MIMEText(_text[, _subtype[, _charset]])

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain.

(Emphasis mine) .

Community
  • 1
  • 1
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
8

This worked for me :)

email_body = """<pre> 
Congratulations! We've successfully created account.
Go to the page: <a href="https://www.google.com/">click here</a>
Thanks,
XYZ Team.
</pre>"""

msg = MIMEText(email_body ,'html')

O/P: Congratulations! We've successfully created account.

Go to the page: click here

Thanks,

XYZ Team.

Walk
  • 1,531
  • 17
  • 21
  • I didn't need the
     tag, and when I sent the email through gmail's python api, the 
     tag changed the look of the font, which wasn't what I was going for. specifying 'html' in the MimeText constructor was sufficient for me
    – TheGaldozer Nov 07 '20 at 03:59