本文介绍了Django使用Google SMTP发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使电子邮件与我的Django应用程序一起使用,但未能成功.我一直在阅读类似的问题,但仍然无法指出我的错误.

I have been trying to get emails working with my Django application and have not been able to. Ive been reading around on similar questions and still haven't been able to pin point my error.

我的settings.py看起来像:

My settings.py looks like :

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email@domain'
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

我发送电子邮件的代码如下:

MY code to send the email looks like :

def application(request):

        if request.method == 'GET':
                form = ApplyForm()
        else:
                form = ApplyForm(request.POST)
                if (form.is_valid()):
                        try:
                                subject = 'Overland Application'
                                from_email = form.cleaned_data['useremail']
                                phone = form.cleaned_data['phone']
                                names = form.cleaned_data['names']
                                year = form.cleaned_data['year']
                                make = form.cleaned_data['make']
                                model = form.cleaned_data['model']
                                message = str(names) + '\n' + str(from_email) + '\n' + str(phone) + '\n' + str(year) + '\n' + str(make) + '\n' + str(model)
                                try:
                                        send_mail(subject, message, settings.EMAIL_HOST_USER, ['email@domain.com'], fail_silently=False)
                                except BadHeaderError:
                                        return HttpResponse('Invalid header found.')
                                return redirect('thanks')
                        except:
                                pass
        return render(request, "overland/apply.html", {'form': form})

一些其他信息是,它似乎正在访问我的电子邮件帐户,因为我确实收到了来自Google的电子邮件,说我的帐户从服务器位置可疑访问.

Some additional information is that it seems to be accessing my email account as I did receive an email from google saying there was suspicious access on my account from the location of the server.

我还从实时服务器ping SMTP服务器,以确保它正在通信.

I also pinged the smtp server from the live server to make sure that it was communicating.

我不确定这是否是我自己某个地方的小语法错误,或者我在使用django邮件功能时使用不正确,因为它在本地似乎可以正常工作,并且会重定向到我的感谢页面,但是当我实时进行此操作时,似乎只是重新加载页面而不发送任何内容.

I am not sure if it is a small syntax error on my part somewhere or I am using the django mail function incorrectly because locally it seemed to work and would redirect to my thanks page, but when I do this live it seems to just reload the page and not send anything.

感谢您提供任何信息.

推荐答案

这是gmail本身的问题.遇到此问题的任何人都应首先尝试进入安全设置并允许访问不太安全的应用程序.如果这样做不起作用,请尝试访问 https://accounts.google.com/DisplayUnlockCaptcha ,然后使用您的再次发送电子邮件的应用程序.

This was an issue with gmail itself. Anybody running into this issue should first try going to security settings and allowing access to less secure apps. if that doesn't work try visiting https://accounts.google.com/DisplayUnlockCaptcha and then use your application to send the email again.

这篇关于Django使用Google SMTP发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:02