Hi, I recently enabled admin email notification of errors in the log configuration for my Django application. I’m using SendGrid as the email client. The email notification is working fine, and I’m noticing an error that I hadn’t seen before. It occurs whenever someone submits a registration form, triggering a confirmation email to be sent to that user.
The emails are being sent, but an error similar to this gets logged and emailed to me each time.
[Django] ERROR (EXTERNAL IP): Invalid HTTP_HOST header: '/run/gunicorn.sock:'. The domain name provided is not valid according to RFC 1034/1035.
webmaster@ifbt.farm <webmaster@ifbt.farm> Fri, Oct 6, 2023 at 5:49 AM
To: dowdrake@gmail.com
Invalid HTTP_HOST header: '/run/gunicorn.sock:'. The domain name provided is not valid according to RFC 1034/1035.
DisallowedHost at /
Invalid HTTP_HOST header: '/run/gunicorn.sock:'. The domain name provided is not valid according to RFC 1034/1035.
Request Method: GET
Request URL: http://url1111.ifbt.farm/ls/click?upn=Rn6uuY2iv1LW59HkWR83rs-2FUAPMrXw42Fzv13-2B1epfz0XUod7LQRyej8P-2BvvoI9-2Bwntc_mEWvXA1o-2FLV9uv02p4M3WSr3lnvTv987Ro1g-2FVuuHiszF6FMD4WbmImEtUNPAY4WM9K3uWvzVpIS8EDSTn1puyxdC0aa4xpOH3PFwTfjrG3D106w1uFWdSod091gCifuZvjy22T-2FJjvUwubBDpG9KUqE-2B8eaA5CYNMUt4uU9Yq55kFUA742915OJ4D47eC6zhotlfDNPAXYEXKabNlUxjw-3D-3D
Django Version: 4.2.5
Python Version: 3.11.4
So the error is raised by the fact that the HOST_HEADER is invalid, but I’m not sure where that header is getting set. I assume it’s set somewhere in the Django code.
The subdomain url1111.ifbt.farm is one of the CNAME records that SendGrid specified to add to my DNS records.
My view passes the request and user to the form code, which sends the email via user.email_user()
The view is:
class RegistrationView(CreateView):
form_class = RegistrationForm
template_name = 'account/register.html'
success_url = reverse_lazy('check_email')
def form_valid(self, form):
to_return = super().form_valid(form)
user = form.save()
user.is_active = False # Turns the user status to inactive
user.save()
form.send_activation_email(self.request, user)
return to_return
and the form is
class RegistrationForm(UserCreationForm):
email = forms.EmailField(
max_length=254, help_text='Enter a valid email address')
class Meta:
model = user_model
fields = [
'username',
'email',
'password1',
'password2',
]
def send_activation_email(self, request, user):
current_site = get_current_site(request)
subject = 'Activate Your Account'
message = render_to_string(
'account/activate_account.html',
{
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': token_generator.make_token(user),
}
)
user.email_user(subject, message, html_message=message)
In my settings, I have
ALLOWED_HOSTS = ['ifbt.farm', 'www.ifbt.farm']
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
Thanks!
Dow