3
votes

Note:- I have checked the question and answer of this post and I have already added default_from_email in my settings as described below. Now, in my contact form I want to receive and email from the users who are trying to contact me.

Hi, I have a blog built in Django which uses Zoho mail to send activation and password reset email. To implement the same, I have added the following code in my settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '<myadmin emailaddress>'
EMAIL_HOST_PASSWORD = '<myadmin password>'
DEFAULT_FROM_EMAIL = '<myadmin email address'

It works flawlessly and the user signing up is getting the activation email and reset emails.

Now, while creating the contact page for my website, I have added a contact form where the user is required to add his name, email and message.

The contact form is like this

class ContactForm(forms.Form):
    name = forms.CharField(max_length=100)
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

The view for the same is :-

def contact_us(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
            sender_name = form.cleaned_data['name']
            sender_email = form.cleaned_data['email']
            message = f"{sender_name} has sent you a new message:\n\n{form.cleaned_data['message']}"
            send_mail('New Enquiry', message, sender_email, ['[email protected]'])
            return HttpResponse('Thanks for contacting us!')
    else:
        form = ContactForm()

    return render(request, 'accounts/contactus.html', {'form': form})

Now, when I am adding the email, message and name, I am getting the following error:-

SMTPDataError at /contact/
(553, b'Relaying disallowed as [email protected]')
1

1 Answers

5
votes

You are trying to get Zoho to send you a message from someone else's email address. That won't work for two reasons:

  • Zoho (like any reputable email provider) won't let you send email pretending to be from someone else. You can only send email from your own account (or the domain you've set up with Zoho). That's why you're getting a "relaying disallowed" error.
  • Even if Zoho would let you send email from other domains, it would end up in spam (or just blocked entirely) at the receiving end. Gmail and most other major email services all have information available listing where email from their addresses can originate. If email comes from somewhere else, it's spam. And your Zoho account is not on their lists! (Search for DMARC, DKIM, and SPF if you're interested in the details.)

So you have to send the message from your own Zoho address, not from the contact's email address.

A frequent goal in contact forms is to be able to easily respond to people who fill out the form. If that's what you're trying to do, the way to achieve it is the Reply-To email header. You'll need to use Django's EmailMessage class to include reply_to (and notice it must be a list, just like to):

from django.core.mail import EmailMessage

# Then to send in your form view...
    contact_name = form.cleaned_data['name']
    contact_email = form.cleaned_data['email']
    message = f"{contact_name} has sent you a new message ..."
    email_msg = EmailMessage(
        subject='New Enquiry', 
        body=message, 
        from_email='[email protected]',  # in your Zoho domain (omit to use DEFAULT_FROM_EMAIL)
        to=['[email protected]'],
        reply_to=[contact_email])  # where you want replies to go
    email_msg.send()

Now when a user fills out your contact form, you'll get a "New Enquiry" email from yourself. And if you Reply to the enquiry, your response will go to the contact's email address.