Django send_mail not working

后端 未结 3 574
误落风尘
误落风尘 2020-12-30 06:41

When the view that sends the email is used nothing happens, i then entered send_mail(...) into the python shell and it returned 1 but i didn\'t receive any emails.

T

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 07:37

    If you don't care Preventing header injection: (you should care about it: https://docs.djangoproject.com/es/1.9/topics/email/#preventing-header-injection, but let's continue)

    The settings.py:

    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_PORT = 587
    EMAIL_HOST_USER = 'user@gmail.com'
    EMAIL_HOST_PASSWORD = 'pass'
    EMAIL_USE_TLS = True
    

    The views.py (example):

    from django.views.generic import View
    from django.core.mail import send_mail
    from django.http import HttpResponse, HttpResponseRedirect
    
    class Contacto(View):
            def post(self, request, *args, **kwargs):
                data = request.POST
                name = data.get('name', '')
                subject = "Thanks  %s !" % (name)
                send_mail(subject, data.get('message', ''), 'user@gmail.com', [data.get('email', '')], fail_silently=False)
            return HttpResponseRedirect('/')
    

    This is a dangerous way to send an email

    When you first try to send the email, you'll receive a google email advising not to do it. You must 'Activate' the 'Less secure apps' (https://www.google.com/settings/security/lesssecureapps) and try again. Second time works.

提交回复
热议问题