I am new to Django coding. I am currently working on DRF Django Rest Framework along with API. I have been trying to send a HTML email template with attachment as soon as the user register . I have achieved sending the same but i want to pass the dynamic contents like email of the user registered to the HTML email template from views.py , But unable to do it. I am using msg.send().
IN VIEWS.PY:
def attachment(request):
queryset = User.objects.all()
em=[]
h=[]
for star in queryset.iterator():
em.append(star.email)
print(em)
h=em[-1]
msg = mail.EmailMultiAlternatives(
subject = 'attachment',
body = 'Hi, Welcome!',
from_email = 'anushstella97@gmail.com',
to = [h],
connection = con
)
msg.attach_file('C:/Users/hp/Downloads/APIV1-master/APIV1-
master/polls/img.jpg')
msg_html = render_to_string('C:/Users/hp/Anaconda3/Lib/site-
packages/allauth/templates/account/email/email_confirmation_message.html'
, {"email": request.user.email})
msg.send()
return HttpResponse('<h1>Created</h1>')
IN HTML TEMPLATE:
<p>Thanks for signing up! We're excited to have you as an early user.</p>
<p> Your registered email is : </p>
<p>
<strong> {{ email }} </strong></p>
To render the template you can use render_to_string
from from django.template.loader
and pass the context variables along with it..
E.g.
from django.template.loader import render_to_string
html = render_to_string("account/email/email_confirmation_message.html", {"email": request.user.email})
Inside email_confirmation_message.html:
Hi {{ email }},
...