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

Django邮件发送异常:邮件未发出且未出现在已发送文件夹

Django发送带PDF附件邮件失败排查问题

环境配置

  • Django版本:4.0.3
  • Python版本:3.12.2

问题描述

代码运行无报错,但收件人未收到邮件,且发送方Gmail账户的“已发送”文件夹中也未出现该邮件。

已尝试操作

  • 验证settings.py中Gmail SMTP服务器的邮件配置正确;
  • 确认指定路径下的PDF文件存在;
  • 确保邮件发送过程未抛出异常;
  • 尝试发送无附件的简单邮件,仍未成功;
  • 检查Gmail账户安全设置,包括启用“低安全性应用访问”并使用应用专用密码。

疑问

  1. 导致邮件未发出且未出现在已发送文件夹的原因可能是什么?
  2. 还有哪些额外的调试步骤可以排查问题?
  3. 是否可能是Gmail SMTP服务器的问题,或是我在Django配置中遗漏了某些内容?

相关代码

def send_email(request, customer_id):
    try:
        order = customer_agent.objects.get(customer_id=customer_id)
    except customer_agent.DoesNotExist:
        return HttpResponseNotFound("Order not found")

    subject = 'Your Order Details'
    message = 'Please find attached the PDF with your order details.'
    recipient_email = order.customer_email
    file_path = f"{settings.BASE_DIR}/orders_data/order_{customer_id}.pdf"

    mail = EmailMessage(subject=subject, body=message, from_email=settings.EMAIL_HOST_USER, to=[recipient_email])
    mail.attach_file(file_path)

    try:
        mail.send()
        print("Email sent successfully!")
    except Exception as e:
        print(f"Failed to send email: {e}")
        return HttpResponse(f"Failed to send email: {e}")

    return HttpResponse("Email sent successfully!")

内容的提问来源于stack exchange,提问作者HizarSajjad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:12:35