0

I am new to Python and Flask, and am getting close to finishing my first project, but I have a question I have run in to. I am sending an email to people who have subscribed to a blog and am wanting to add a link to allow them to unsubscribe if the click the link. My email is sent through smtplib. I have a working link currently using itsdangerous, but I was wondering how I can change the link from an actual url, into a hyperlinked text. For instance someone can click "click me" and it takes them to that same link.


token = s.dumps(subscriber_email, salt='unsubscribe')

link = url_for('views.unsubscribe', token=token, _external=True, _scheme="click here.")

subscriber_message = "You have been subscribed. Thank you.\n\n\n\n\n\n\n\nIf you wish to be removed from the subscription please " + "click here {}".format(link)

The link currently works, I would just like to clean up what the user sees. Here is the email the user currently sees:

You have been subscribed. Thank you.

If you wish to be removed from the subscription please click here http://127.0.0.1:5000/.../ImR1c3Rpbm9saXZlcjEyQGdtYWlsLmN...

If anyone has any thoughts I would certainly appreciate it.

Dustin916
  • 1
  • 1

1 Answers1

0

you need to use html tags to make the url clickable in email.

Here is a example:

email_body = """
<pre> 
You have been subscribed. Thank you.

If you wish to be removed from the subscription please click here:
<a href="http://127.0.0.1:5000/.../ImR1c3Rpbm9saXZlcjEyQGdtYWlsLmN">unsubscribe</a>
</pre>
"""

output:

 
You have been subscribed. Thank you.
    
If you wish to be removed from the subscription please click here:
unsubscribe
 

In your code it should be like this:

token = s.dumps(subscriber_email, salt='unsubscribe')

link = url_for('views.unsubscribe', token=token, _external=True, _scheme="click here.")

subscriber_message = "<pre>You have been subscribed. Thank you.\n\n\n\n\n\n\n\nIf you wish to be removed from the subscription please " + 'click here <a href="{}">unsubscribe</a></pre>'.format(link)
rafathasan
  • 524
  • 3
  • 15
  • Thank you, but when it seems the email (gmail) does not interpret the html and just outputs it instead. – Dustin916 Oct 19 '22 at 03:28