from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from app.forms import ContactForm
from django.views.generic import TemplateView
from django.conf import settings
import django

class IndexView(TemplateView):
    template_name = 'index.html'

def email(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(subject, from_email+'가 보냄\n\n'+message, from_email, ['roharon@naver.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('thanks')

    return render(request, "email.html", {'form': form})

def thanks(request):
    return HttpResponse('전달 완료')

def index(request):
    return render(request, "index.html")
