【问题标题】:django mail sending very slowly.django 邮件发送速度很慢。
【发布时间】:2013-02-27 12:55:11
【问题描述】:

我在 django 中使用 EmailMessage 发送邮件。这是我的代码:

  message = " test message "
  email = EmailMessage('test subject', message, to=['me@test.com'])
  email.send()

但是我需要向不同的人发送更多不同内容的电子邮件,所以如果我做了两次以上的操作,发送邮件需要很长时间。比如 10 秒或更长时间。是否有任何其他解决方案可以更轻松快捷地发送?

在 php 中它非常快。我也可以在 django 中有这个吗?

非常感谢。

【问题讨论】:

  • 使用 send_mail() 代替
  • @catherine,但它仍然很慢。不是因为我在本地主机吗?
  • 我会搜索你的问题
  • @catherine,太好了!谢谢
  • 在我的情况下是 ipv6 问题,在 /etc/hosts 文件中添加 smtp 服务器的 ipv4 地址已解决。

标签: python django email sendmail


【解决方案1】:

如果您需要发送多封电子邮件并希望加快流程,可以使用 Django 的send_mass_mail 功能。该文档非常清楚地说明了如何使用它;这是一个示例表单:

message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)

这将为所有电子邮件重用一个连接。

【讨论】:

  • 但仍然需要 11 秒。但是我还在本地主机,这会是发送缓慢的原因吗?
  • @doniyor 然后你将不得不做一些调试。一个起点可以是检查邮件服务器上的日志和应用程序中的日志并寻找差异。
【解决方案2】:

@Thomas Orozco 答案的扩展。

django-pigeonpost - 允许您设置发送邮件的时间等等...

https://github.com/dragonfly-science/django-pigeonpost

django cron - 从 webapp 的第一个请求开始,它将每 300 毫秒(0.3 秒)发送一次电子邮件

https://sites.google.com/site/vigeblog/blog/sendmailswithdjango

如果发送邮件再次变慢(在您看来),您必须检查服务器、重构代码并进行测试。

【讨论】:

    猜你喜欢
    • 2013-01-02
    • 2015-06-12
    • 1970-01-01
    • 2013-12-02
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多