0

I want to sent the link to the email so when user clicks that link, the user will be redirected to the refer page and can refer other friends. I have used send_mail to send the email. Everything gets sent except the html message. Here is what i have done

  if created:
     new_join_old.invite_code = get_invite_code()
     new_join_old.save()
     subject = "Thank you for your request to sign up our community"
     html_message = '<a href="http://localhost:8000/{% url "invitations:refer-invitation" invite_code %}">Click Here</a>'
     message = "Welcome! We will be in contact with you."
     from_email = None
     to_email = [email]
     send_mail(subject, message, from_email, to_email, fail_silently=True, html_message=html_message)
     messages.success(request, '{0} has been invited'.format(email))
   return HttpResponseRedirect("/invitations/refer-invitation/%s"%(new_join_old.invite_code))
context = {"form": form}
return render(request, 'home.html', context)
pythonBeginner
  • 781
  • 2
  • 12
  • 27

2 Answers2

1

[UPDATE]: In order for the below to work you must have set then appropriate email settings inside your settings.py file, like this:

# settings.py

#######################
#   EMAIL SETTINGS    #
#######################
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email_username_here'
EMAIL_HOST_PASSWORD = 'email_password_here'

The <a> link is not rendered because there is no loader to render it (unknown {% url %} template tag). If you like to preserve this syntax ({% url ... %}) and have a separate HTML file that will be sent then store the HTML file as separate file, say html_email.html and then use render_to_string and do the following:

<!-- html_email.html -->

<a href="http://localhost:8000/{% url 'invitations:refer-invitation' invite_code %}">Click Here</a>


# views.py

from django.template.loader import render_to_string

if created:
    # above code as is
    # in the context you can pass other context variables that will be available inside the html_email.html
    context = {'invite_code': new_join_old.invite_code,}
    html_message = render_to_string('path/to/html_email.html', context=context)
    # below code as is

or you can do it this way:

#views.py

from django.core.mail import EmailMultiAlternatives
from django.urls import reverse

if created:
    # above code as is
    html_message = '<a href="http://localhost:8000{}">Click Here</a>'.format(reverse('invitations:refer-invitation', kwrags={'invite_code': invite_code}))
    msg = EmailMultiAlternatives(subject, message, from_email, to_email, fail_silently=True)
    msg.attach_alternative(html_message, 'text/html')
    msg.send()
nik_m
  • 11,825
  • 4
  • 43
  • 57
  • I did this already_join.save() subject = "Thank you for your request to sign up our community" context = {'invite_code': already_join.invite_code} html_message = render_to_string('email_templates/email.html', context=context) message = "Welcome! We will be in contact with you." from_email = None to_email = [email] send_mail(subject, message, from_email, to_email, fail_silently=True, html_message=html_message) but is not working yet. Subject and message is sent but not html_message – pythonBeginner Mar 20 '17 at 16:28
  • @pythonLover Updated my answer with the appropriate email settings that must be set. Are you sure are set? Because I just tested it and works. – nik_m Mar 20 '17 at 17:02
  • sorry for late response. I am using celery for asynchrnonous task queue. I have updated my question with celery code and i am using MAILGUN. – pythonBeginner Mar 21 '17 at 01:10
  • I think I am missing something in my EmailBackend. It is still not working. – pythonBeginner Mar 21 '17 at 12:21
  • Inside the `EmailBackend`'s `__init__` method change to this: `super().__init__(self, **kwargs)`. – nik_m Mar 21 '17 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138638/discussion-between-pythonlover-and-nik-m). – pythonBeginner Mar 21 '17 at 12:41
0
from django.template import Context, Template

email_data = open('email_templates/email.html', 'r').read()
html_data = Template(email_data)
html_content = html_data.render(Context({'invite_code': new_join_old.invite_code}))

You can add message,subject etc key-value pair in context as well

In email.html

<!DOCTYPE html>
<html>

<head>
</head>
<body>
    <a href="http://localhost:8000/invitations/refer-invitation/{{ invite_code }}">Click Here</a>
</body>
</html>

You can access the value of context variable like message and subject in email.html using {{ message }} and {{ subject }}

https://docs.djangoproject.com/en/1.10/topics/email/#the-emailmessage-class

Ajay Singh
  • 1,251
  • 14
  • 17