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\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

My first guess is that your email host doesn’t accept the format 'Mydomain <info@mydomain.com>' as the sender’s email. I’d try changing it to info@mydomain.com 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 = 'mail.mydomain.com' in my settings file. And I get this address from my cPanel. Also the same error still keeps when I write DEFAULT_FROM_EMAIL = 'info@mydomain.com' . Again so so sorry about miswrote. :pray:

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

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/iletisim/

Django Version: 5.0.2
Python Version: 3.11.4
Installed Applications:
['jazzmin',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'whitenoise.runserver_nostatic',
 'django.contrib.staticfiles',
 'pages.apps.PagesConfig',
 'portfolio.apps.PortfolioConfig',
 'services.apps.ServicesConfig',
 'blog.apps.BlogConfig',
 'ckeditor']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\avaweb_con\pages\views.py", line 30, in contact
    return render(request, 'contact.html', {'form': form,})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\shortcuts.py", 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\loader.py", 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\django.py", line 61, in render
    return self.template.render(context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\base.py", line 169, in render
    with context.bind_template(self):
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\template\context.py", line 254, in bind_template
    context = processor(self.request)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\avaweb_con\pages\context_processors.py", line 15, in mailsubs
    send_mail(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\__init__.py", line 88, in send_mail
    return mail.send()
           ^^^^^^^^^^^
  File "C:\Users\Bedirhandd\Videos\ayva_web_project\env\Lib\site-packages\django\core\mail\message.py", 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\smtp.py", 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\smtp.py", line 155, in _send
    self.connection.sendmail(
    ^
  File "C:\Users\Bedirhandd\AppData\Local\Programs\Python\Python311\Lib\smtplib.py", 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?