我正在尝试向用户发送邮件,但是django send_mail()通过错误起作用。
我和yahoo一样尝试了gmail smtp屁股。但无法找到问题所在?表格可以完美地工作,任何查询都可以将数据存储到数据库中。
这是我的views.py文件
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)
这是我的settings.py文件:
## 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
“ SMTPServer在/ contacts / contact处断开连接
连接意外关闭
请求方法:POST
要求网址:http://127.0.0.1:8000/contacts/contact
Django版本:2.2.2
异常类型:SMTPServerDisconnected
异常值:
连接意外关闭
异常位置:getreply中的c:\ users \ user \ appdata \ local \ programs \ python \ python37-32 \ Lib \ smtplib.py,行394
Python可执行文件:C:\ Users \ USER \ Desktop \ Django \ realstate \ venv \ Scripts \ python.exe
的Python版本:3.7.0
Python路径:
['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']
服务器时间:2019年8月4日,星期日,19:15:47 +0000“