# crm/urls.py
from django.urls import path
from backend.crm import views as v
app_name = 'crm'
urlpatterns = [
path('contact/', v.send_contact, name='send_contact'), # noqa E501
]
Edite crm/forms.py
# crm/forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField('nome', max_length=255)
email = forms.EmailField('email',) # sender
title = forms.CharField('título', max_length=100) # subject
body = forms.CharField(widget=forms.Textarea) # message
Edite crm/views.py
crm/views.py
from django.core.mail import send_mail
from django.shortcuts import redirect
from django.views.decorators.http import require_http_methods
from .forms import ContactForm
@require_http_methods(['POST'])
def send_contact(request):
form = ContactForm(request.POST or None)
if form.is_valid():
subject = form.cleaned_data.get('title')
message = form.cleaned_data.get('body')
sender = form.cleaned_data.get('email')
send_mail(
subject,
message,
sender,
['localhost'],
fail_silently=False,
)
return redirect('core:index')