I am working on a Django project. I have a contact form. What I want when someone fills up the form my info @ mydomain .com should send my gmail a notification like “Someone filled up your form”. Which is funny the notification email is successfully coming to my gmail! Everything works fine but the error still occures! I am newbie so i would like a help so much! Thank you already!
When i go to contact form and submit it, there is an error page that says:
SMTPSenderRefused at /contact/ (501, b’<settings.EMAIL_HOST_USER>: sender address must contain a domain’, ‘settings.EMAIL_HOST_USER’)
-
Django Version: 5.0.2
-
Exception Type: SMTPSenderRefused
-
Exception Value: (501, b’<settings.EMAIL_HOST_USER>: sender address must contain a domain’, ‘settings.EMAIL_HOST_USER’)
-
Exception Location: C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\smtplib.py, line 887, in sendmail
And here is my codes:
Settings.py
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_BACKEND = 'backend.email.EmailBackend'
EMAIL_HOST = 'mail.mydomain.com' #Edited, I miswrote when I posted Question, error still occures
EMAIL_PORT = 587
EMAIL_HOST_USER = 'info@mydomain.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'Mydomain <info@mydomain.com>'
backend.email file:
(I created the email_backend.py file to avoid “[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain” error in cPanel., error still occures when i use the default EMAIL_BACKEND)
I found the code here
import ssl
from django.core.mail.backends.smtp import EmailBackend as SMTPBackend
from django.utils.functional import cached_property
class EmailBackend(SMTPBackend):
@cached_property
def ssl_context(self):
if self.ssl_certfile or self.ssl_keyfile:
ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_cert_chain(self.ssl_certfile, self.ssl_keyfile)
return ssl_context
else:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
return ssl_context
And lastly here is my views.py:
from django.shortcuts import render
from portfolio.models import Portfolio
from services.models import Service
from .forms import ContactFormForm
from django.core.mail import send_mail
from django.contrib import messages
from django.urls import reverse_lazy
from avaweb.settings import DEFAULT_FROM_EMAIL
def contact(request):
if request.method == 'POST':
form = ContactFormForm(request.POST)
if form.is_valid():
form.save()
subject = f'New Form Filled:: {form.cleaned_data["subject"]}'
message = 'New Form Filled:. Details down below:\n\n' + \
f'Username: {form.cleaned_data["name"]}\n' + \
f'Service: {form.cleaned_data["subject"]}\n' + \
f'How Many Workers: {form.cleaned_data["workers"]}\n' + \
f'Users Mail: {form.cleaned_data["email"]}\n' + \
f'Phone: {form.cleaned_data["phone"]}\n' + \
f'When to call?: {form.cleaned_data["call_time"]}\n' + \
f'Message: {form.cleaned_data["message"]}\n'
send_mail(subject,
message,
DEFAULT_FROM_EMAIL,
['mygmail@gmail.com'],
fail_silently=False
)
messages.success(request, 'Thank you!')
else:
form = ContactFormForm()
return render(request, 'contact.html', {'form': form,})
Edit: I am using cPanel for my info @ mydomain .com