Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add href link in email content when sending email through smtplib

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>
like image 935
Yuwen Yan Avatar asked Jul 30 '15 04:07

Yuwen Yan


People also ask

How do I send an email using Smtplib?

Python offers a ` library to send emails- “SMTP lib”. “smtplib” creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. The Port number used here is '587'.

How do you make a link clickable in Python?

link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination.


2 Answers

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.

like image 34
Walk Avatar answered Sep 28 '22 19:09

Walk


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) .

like image 50
Anand S Kumar Avatar answered Sep 28 '22 20:09

Anand S Kumar