django send_mail()函数需要几分钟 [英] django send_mail() function taking several minutes

查看:203
本文介绍了django send_mail()函数需要几分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过views.py文件中的功能发送电子邮件.我已经按照与此处相同的方式在我的设置文件中设置了电子邮件.

I'm trying to send emails in a function within my views.py file. I've set up the email in my settings file in the same manner as here.

Python Django Gmail SMTP设置

电子邮件发送确实可以,但是我的用户一直在抱怨几分钟.我在我的var/log/mail.log文件中收到一个gethostbyaddress错误,该错误将在此处发布.我过去经常收到nginx超时错误,但是却输入了"proxy_read_timeout 150;".到我的/etc/nginx/sites-enabled/django文件中.

Email sending does work but it takes several minutes to occur which my users have been complaining about. I am receiving a gethostbyaddress error in my var/log/mail.log file which I'll post here. I used to get nginx timeout errors but put "proxy_read_timeout 150;" into my /etc/nginx/sites-enabled/django file.

这解决了与网站交互时的超时错误,但是电子邮件仍然需要几分钟的时间才能加载.我使用的是Digitalocean django小滴,这种缓慢的速度发生在我所有的小滴上.

This solved the timeout errors when interacting with the website but the emails still take several minutes to load. I'm using a digitalocean django droplet and this slow speed has occured on all my droplets.

这是我的视图功能

@login_required
def AnnouncementPostView(request, leaguepk):
    league = League.objects.get(pk=leaguepk)
    lblog = league.blog

    if request.method == 'POST':
        form = AnnouncementPostForm(request.POST)
        if form.is_valid():
            posttext = request.POST['text']

            newAnnouncement = Announcement(text=posttext, poster=request.user)
            newAnnouncement.save()
            lblog.announce.add(newAnnouncement)

            titleText = "%s Announcement" % (league.name)

            send_mail(titleText, posttext, settings.EMAIL_HOST_USER, ['mytestemail@gmail.com'], fail_silently=False)

       return HttpResponseRedirect(reverse('league-view', args=[league.pk]))
else:
    form = AnnouncementPostForm()

return render(request, 'simposting/announcementpost.html', {'form': form, 'league': league})

这已经奏效,公告已发布到所需的页面上,甚至通过电子邮件发送,这只是一个时间问题,人们已经开始期待几乎即时的电子邮件发送过程,这使得2-3分钟是不可接受的,尤其是在注册时也会导致2-3分钟的等待时间.

This has worked, the announcement is posted to the desired page and is even emailed, it's just a time problem, people have come to expect nearly instant emailing processes which makes the 2-3 minutes unacceptable, especially when signing up also causes the 2-3 minute wait.

一个问题可能是,在与DigitalOcean支持团队一起解决此问题时,我将我的Droplet名称和主机名更改为我设置的域.

One issue may be the fact that while trying to solve this issue with the DigitalOcean support team I changed my droplet name and the hostname to be the domain that I set up.

我当前的主机名和Droplet名称是mydomain.com.我在/etc/hostname文件中以这种方式进行设置.我的/etc/hosts文件看起来像这样

My current hostname and droplet name is mydomain.com. I have it setup that way in my /etc/hostname file. My /etc/hosts file looks like this

127.0.0.1 localhost.localdomain localhost mydomain.com
127.0.1.1 mydomain.com

每当我尝试发送邮件时,我的var/log/mail.log文件都会对此进行响应

My var/log/mail.log file responds with this whenever I try to send mail

Oct 6 16:13:24 "oldDropletName" sm-mta[13660]: gethostbyaddr(10.xxx.xx.x) failed: 1
Oct 6 16:13:24 "oldDropletName" sm-mta[13662]: starting daemon (8.14.4): SMTP+queueing@00:10:00

我希望这是足够的信息,已经困扰了好几个星期,通常我可以通过在这里查找内容或与支持团队合作来解决自己的问题,但让我们感到困惑.感谢您抽出宝贵的时间来帮助您!

I hope this is enough information to help, it's been troubling for several weeks and usually I can either solve my problems by looking up stuff here or working with the support team but it's got us stumped. Thank you for taking the time to help!

推荐答案

发送电子邮件是一项与网络相关的任务,您不知道要花多少时间才能完全符合您的情况.尽管网络中可能存在延迟,但是最好以异步方式执行此类任务,以便您的主线程是空闲的.

Sending an email is a network bound task and you don't know how long it will take to finish exactly like in your case. Although there might be a latency in your network but it's better to do such task in an async fashion so your main thread is free.

我在我的项目中使用以下代码.

I am using the following code in one my projects.

utils.py

import threading
from django.core.mail import EmailMessage


class EmailThread(threading.Thread):
    def __init__(self, subject, html_content, recipient_list, sender):
        self.subject = subject
        self.recipient_list = recipient_list
        self.html_content = html_content
        self.sender = sender
        threading.Thread.__init__(self)

    def run(self):
        msg = EmailMessage(self.subject, self.html_content, self.sender, self.recipient_list)
        msg.content_subtype = 'html'
        msg.send()


def send_html_mail(subject, html_content, recipient_list, sender):
    EmailThread(subject, html_content, recipient_list, sender).start()

只需从您的视图调用send_html_mail.

just call send_html_mail from your view.

这篇关于django send_mail()函数需要几分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆