You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django邮件发送失败且无控制台错误提示问题求助

Django注册表单提交后无法发送确认邮件的排查与修复

问题现象

实现注册表单提交后发送确认邮件功能时,代码无法发送邮件,控制台无明确错误提示,甚至代码末尾的print("email sent")语句都没有输出。

提供的代码片段

views.py

@require_POST
def thanks(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        phone= request.POST['phone']
        doctor = request.POST['doctor']
        msg = request.POST['reason']
        print(name, email, phone, msg)
        appointment_db = Appointment(name=name, email=email, phone=phone, doctor=doctor, 
        message=msg)
        appointment_db.save()

    subject = "Thank you for registering"
    message = "Welcome the the clinic."
    from_email = settings.EMAIL_HOST_USER
    recipient_list = [email]
    send_mail(subject, message, from_email, recipient_list)
    print("email sent")
    return render(request, "thanks.html", {"name": name})

settings.py

EMAIL_BACKEND= "django.core.mail.backends.smpt.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT= 587
EMAIL_HOST_USER = "host email" # email from which i sent email
EMAIL_HOST_PASSWORD = "host password" #password of the host account

views.py导入语句

from django.core.mail import send_mail
from django.conf import settings

问题排查与修复方案

1. 缩进错误导致邮件逻辑未执行

视图已使用@require_POST装饰器,仅POST请求会进入视图,但邮件发送代码未缩进在if request.method == 'POST':代码块内。当请求为POST时,name、email等变量仅在if块内定义,if块外直接调用recipient_list = [email]会触发NameError,导致代码中断,无法执行到print("email sent")。

修复:将邮件发送相关代码缩进至if块内部:

@require_POST
def thanks(request):
    if request.method == 'POST':
        name = request.POST['name']
        email = request.POST['email']
        phone= request.POST['phone']
        doctor = request.POST['doctor']
        msg = request.POST['reason']
        print(name, email, phone, msg)
        appointment_db = Appointment(name=name, email=email, phone=phone, doctor=doctor, 
        message=msg)
        appointment_db.save()

        # 邮件发送代码缩进至此
        subject = "Thank you for registering"
        message = "Welcome to the clinic."  # 修正拼写错误:the the → to the
        from_email = settings.EMAIL_HOST_USER
        recipient_list = [email]
        send_mail(subject, message, from_email, recipient_list)
        print("email sent")
    return render(request, "thanks.html", {"name": name})

2. SMTP拼写错误

settings.py中EMAIL_BACKEND的值拼写错误,smpt应为smtp,导致Django无法找到正确的邮件后端:

# 修正前
EMAIL_BACKEND= "django.core.mail.backends.smpt.EmailBackend"
# 修正后
EMAIL_BACKEND= "django.core.mail.backends.smtp.EmailBackend"

3. Gmail SMTP配置注意事项

若使用Gmail作为邮件发送服务器:

  • 若开启了两步验证,需使用应用专用密码作为EMAIL_HOST_PASSWORD,而非普通账户密码
  • 若未开启两步验证,需在Google账户设置中开启"不太安全的应用访问"(不推荐,建议开启两步验证并使用应用密码)
  • 确保EMAIL_HOST_USER为完整的邮箱地址,例如yourname@gmail.com

4. 添加异常捕获便于排查

在邮件发送逻辑中添加异常捕获,可直观看到错误信息:

# 补充导入异常类
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse

# 在邮件发送部分替换为以下代码
try:
    send_mail(subject, message, from_email, recipient_list)
except BadHeaderError:
    return HttpResponse('存在无效请求头。')
except Exception as e:
    print(f"邮件发送失败:{str(e)}")
    return HttpResponse('邮件发送失败,请稍后重试。')

内容的提问来源于stack exchange,提问作者Abdul Moiz Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 16:12:50