0

I am trying to send mailto my user but django send_mail() function through an error.

I tried gmail smtp ass well as yahoo. but unable to find what the problem is?? Form is perfectly working and data goes to database whenever any enquiry made.

This is my views.py file

from django.shortcuts import render, redirect
from django.contrib import messages
from django.conf import settings
from .models import Contact
from django.core.mail import send_mail
# Create your views here.


def contact(request):
    if request.method == 'POST':
        listing_id = request.POST['listing_id']
        listing = request.POST['listing']
        name = request.POST['name']
        phone = request.POST['phone']
        email = request.POST['email']
        message = request.POST['message']
        user_id = request.POST['user_id']
        realtor_email = request.POST['realtor_email']
        contact = Contact(listing_id=listing_id, listing=listing, name=name, phone=phone, email=email, message=message, user_id=user_id)

        # if user already made enquiry for this listing
        if request.user.is_authenticated:
            user_id = request.user.id
            has_contacted = Contact.objects.all().filter(listing_id=listing_id, user_id=user_id)
            if has_contacted:
                messages.error(request, 'You are already made enquiry for this listing.')
                return redirect('/listings/' + listing_id)
        contact.save()

        ## SEnd Mail
        from_email = settings.EMAIL_HOST_USER
        send_mail(
            'Property Listing Enquiry',
            'There has been a entry for ' + listing + 'Sign Into the admin panel for more info.',
            from_email,
            [realtor_email, 'ramji1493@gmail.com'],
            fail_silently=False,
        )

        messages.success(request, 'Your message has been submitted. A realtor will get back to you soon.')
        return redirect('/listings/'+listing_id)

This is my settings.py file:


## Email Configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.mail.yahoo.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'pymayank@yahoo.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_USE_TLS = True

"SMTPServerDisconnected at /contacts/contact Connection unexpectedly closed Request Method: POST Request URL: http://127.0.0.1:8000/contacts/contact Django Version: 2.2.2 Exception Type: SMTPServerDisconnected Exception Value:
Connection unexpectedly closed Exception Location: c:\users\user\appdata\local\programs\python\python37-32\Lib\smtplib.py in getreply, line 394 Python Executable: C:\Users\USER\Desktop\Django\realstate\venv\Scripts\python.exe Python Version: 3.7.0 Python Path:
['C:\Users\USER\Desktop\Django\realstate', 'C:\Users\USER\Desktop\Django\realstate\venv\Scripts\python37.zip', 'C:\Users\USER\Desktop\Django\realstate\venv\DLLs', 'C:\Users\USER\Desktop\Django\realstate\venv\lib', 'C:\Users\USER\Desktop\Django\realstate\venv\Scripts', 'c:\users\user\appdata\local\programs\python\python37-32\Lib', 'c:\users\user\appdata\local\programs\python\python37-32\DLLs', 'C:\Users\USER\Desktop\Django\realstate\venv', 'C:\Users\USER\Desktop\Django\realstate\venv\lib\site-packages'] Server time: Sun, 4 Aug 2019 19:15:47 +0000"

  • 1
    You should google your problem first before asking. You'll find various possible solutions to your problem, e.g. creating an app-specific password if you use 2FA or unlocking the request from an unknown location. – dirkgroten Aug 05 '19 at 12:07
  • I tried with many gmail account and yahoo mail account as SMTP host with or without 2FA verification. Everytime it gives the same error. Pls suggest.. – Mayank Porwal Aug 09 '19 at 15:07
  • Add the DEFAULT_FROM_EMAIL setting to be 'pymayank@yahoo.com' – dirkgroten Aug 09 '19 at 15:17

0 Answers0