How to add href link to email content when sending email via smtplib

I am sending email through the code below:

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 get

abc

what i really got:

 <a href="www.google.com">abc</a> 
+5
source share
1 answer

You must specify 'html' as a subtype -

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

Without specifying a subtype separately, the default subtype is 'plain' (plain-text). From the documentation -

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

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects in the body text. _text is a string for the payload. _subtype is a minor type and is equal by default.

(My emphasis).

+3
source

All Articles