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

前端 未结 2 1688
忘了有多久
忘了有多久 2020-12-31 05:29

I\'m sending email through below code:

msg = MIMEText(u\'abc\')
msg[\'Subject\'] = \'subject\'
msg[\'From\'] = \'x         


        
相关标签:
2条回答
  • 2020-12-31 05:52

    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.

    0 讨论(0)
  • 2020-12-31 05:58

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

    0 讨论(0)
提交回复
热议问题