【问题标题】:Send_mail in Django, works in shell, works locally, not in viewDjango中的Send_mail,在shell中工作,在本地工作,不在视图中
【发布时间】:2012-10-12 16:17:56
【问题描述】:

我什至不确定如何调试它:我在 Django 的一个视图中使用 send_mail。在本地使用应用程序时它工作正常(使用我在生产中使用的相同 SMTP 设置),它在生产中的 shell 工作正常(再次,使用相同的设置)。但是当我实际在生产中使用该应用程序时,消息不会发送。知道如何排除故障吗?

我不会粘贴整个视图,但这里是相关部分。

if request.method == 'POST':
    message, form = decideform(request.POST)
    if form.is_valid():
        invitation.status = form.cleaned_data['status']
        invitation.save()
        message, form = decideform(request.POST)
        update = 'Thank you for updating your status.'

        # Send an email confirming their change
        subject = 'Confirming your RSVP [%s %s]' % (invitation.user.first_name, invitation.user.last_name)
        body = message + ' Remember, the URL to update or change your RSVP is http://the.url%s.' % invitation.get_absolute_url()
        send_mail(subject, body, 'rsvp@mydomain.com', ['rsvp@mydomain.com', invitation.user.email], fail_silently=True)

这是我的 local_settings 文件中的相关位,为了安全起见,对重要的细节进行了更改:

EMAIL_HOST = 'mail.mydomain.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'rsvp@mydomain.com'
DEFAULT_FROM_USER = 'rsvp@mydomain.com'
EMAIL_HOST_PASSWORD = 'p4ssw0rd'

【问题讨论】:

  • 欢迎来到 SO!您是否尝试过使用fail_silently=False 并寻找任何SMTPException
  • 谢谢你,塞萨尔!我确实尝试了将 fail_silently 标志设置为 False(在本地、在生产 shell 中以及从生产中的应用程序),并且在任何时候都没有捕获任何 SMTPExceptions。 (我假设生产中的应用程序会将该异常写入错误日志。)

标签: django email send


【解决方案1】:

检查权限。可能是您有权运行 send_mail,但应用程序没有。

【讨论】:

    【解决方案2】:

    我也有同样的问题。 3年后:) 复制 send_mail inline 的代码修复了它,但我仍然不知道为什么

            connection = django.core.mail.get_connection(username=None, password=None, fail_silently=False)
            mail = django.core.mail.message.EmailMessage('hello', 'world', 'test@gmail.com', ['test@gmail.com'],                                              connection=connection)
            mail.send()            
    

    【讨论】:

      【解决方案3】:

      你可以试试 django-sendgrid,它很容易配置。使用这样的设置

      EMAIL_BACKEND = "sgbackend.SendGridBackend"
      SENDGRID_USER = 'xxxxxx'
      SENDGRID_PASSWORD = 'xxxxxx'
      EMAIL_PORT = 1025
      

      【讨论】:

        猜你喜欢
        • 2015-06-25
        • 1970-01-01
        • 2012-04-14
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 2021-01-08
        • 1970-01-01
        相关资源
        最近更新 更多