SMTPSenderRefused Error Django: (501, b'<settings.EMAIL_HOST_USER>: sender address must contain a domain', 'settings.EMAIL_HOST_USER')

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\, line 887, in sendmail

And here is my codes:

# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = '' #Edited, I miswrote when I posted Question, error still occures
EMAIL_HOST_PASSWORD = 'mypassword' 
DEFAULT_FROM_EMAIL = 'Mydomain <>' file:

(I created the 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):
    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
            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

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():
            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' 

            messages.success(request, 'Thank you!')
        form = ContactFormForm()
    return render(request, 'contact.html', {'form': form,})

Edit: I am using cPanel for my info @ mydomain .com

My first guess is that your email host doesn’t accept the format 'Mydomain <>' as the sender’s email. I’d try changing it to to see if that resolves this.

It didn’t work :frowning: , actually I tried every odds. I used EMAIL_HOST_USER or DEFAULT_FROM_EMAIL in several ways but none of them work. But thank you a lot anyway, any more idea?

Please confirm that when you correct the DEFAULT_FROM_EMAIL setting as identified above that you’re still getting the same error code. (Note, it would be helpful to see the corrected setting you are using along with the complete traceback and error message.)

This is something else that doesn’t look right to me. The EMAIL_HOST setting is looking for a host name, not an email address. (However, I don’t see how that would cause the error message you’re reporting.)

1 Like

I am so sorry, I miswrote the code in the question. It is EMAIL_HOST = '' in my settings file. And I get this address from my cPanel. Also the same error still keeps when I write DEFAULT_FROM_EMAIL = '' . Again so so sorry about miswrote. :pray:

Here is my traceback from error page, hope it helps:


Request Method: POST
Request URL:

Django Version: 5.0.2
Python Version: 3.11.4
Installed Applications:
Installed Middleware:

Traceback (most recent call last):
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\handlers\", line 55, in inner
    response = get_response(request)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\handlers\", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\avaweb_con\pages\", line 30, in contact
    return render(request, 'contact.html', {'form': form,})
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\", line 25, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\backends\", line 61, in render
    return self.template.render(context)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\", line 169, in render
    with context.bind_template(self):
  File "C:\Users\Bedirhandd\AppData\Local\Programs\Python\Python311\Lib\", line 137, in __enter__
    return next(self.gen)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\", line 254, in bind_template
    context = processor(self.request)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\avaweb_con\pages\", line 15, in mailsubs
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\", line 88, in send_mail
    return mail.send()
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\", line 300, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\backends\", line 136, in send_messages
    sent = self._send(message)
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\backends\", line 155, in _send
  File "C:\Users\Bedirhandd\AppData\Local\Programs\Python\Python311\Lib\", line 887, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)

Exception Type: SMTPSenderRefused at /iletisim/
Exception Value: (501, b'<settings.EMAIL_HOST_USER>: sender address must contain a domain', 'settings.EMAIL_HOST_USER')

What is your current setting for EMAIL_HOST_USER?