lettertofcps/pages/views.py
Rushil Umaretiya 8d6926b085 oops
2020-09-26 20:09:49 -04:00

40 lines
1.3 KiB
Python

from django.shortcuts import render
from random import choice
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from config.settings import EMAIL_HOST_USER
from django.core.mail import EmailMultiAlternatives, send_mail
# Create your views here.
def main(request):
if request.POST:
subjects = [
'A letter listing my concerns',
'Concerns with the recent admissions proposal',
"Concerns with FCPS' proposal to change the TJ admissions system"
]
subject = choice(subjects)
context = {
'form':request.POST,
'mailto': f'mailto:{",".join(request.POST.getlist("rep"))}?subject={subject}'
}
html_message = render_to_string('pages/email_template.html', context=context)
plain_message = strip_tags(html_message)
#recepient = request.POST.getlist('rep')
recepient = ['rushilwiz@gmail.com', 'lettertofcps@gmail.com']
sender = [request.POST.get('email')]
email = EmailMultiAlternatives(
subject,
plain_message,
EMAIL_HOST_USER,
recepient,
cc=sender,
reply_to=sender
)
email.attach_alternative(html_message, "text/html")
print("### EMAIL SENT ###")
email.send(fail_silently=False)
return render (request, "pages/email.html", context=context)
return render(request, "pages/index.html")