sinθ
sinθ

Reputation: 11493

Links not clickable when sent through Django email

When the website sends out emails, it using a a tag to create a link. For some reason, gmail (possible other emails as well) does not turn the text the a tag contains into a link.

Function for sending emails:

def send_main(to, sbj, msg):
    msg = EmailMultiAlternatives(sbj, msg, '********@gmail.com', to)
    msg.content_subtype = "html"
    msg.send()

Paramaters passed to email in shell

send_main(['****@gmail.com'], 'test', '<a href="http://www.google.com"/>test</a>')

Yet when I execute the line in the python shell, it sends the email but gmail does not recognize the link.

Upvotes: 3

Views: 2735

Answers (2)

vipin
vipin

Reputation: 678

it is not clickable because some mailing site will block href, but this will work in gmail and to make the link clickable in those site use send_mail instead

from django.core.mail import send_mail

send_mail('Subject', 'http://www.google.com', '[email protected]',
['[email protected]'], fail_silently=False)

hope this will work

Upvotes: 2

josephmisiti
josephmisiti

Reputation: 9974

Use this method:

html_content = render_to_string('emails/email_%s.html' % template_name, { parmas })
message = EmailMultiAlternatives(subject, html_content, settings.GENERIC_EMAIL_SENDER,[email])
message.attach_alternative(html_content, 'text/html')
message.send()

Pass it into a HTML template and call render_to_string - make sure the attachment is 'text/html' and it should work fine.

Upvotes: 1

Related Questions