Django 1.8 sending mail using gmail SMTP

前端 未结 7 875
旧巷少年郎
旧巷少年郎 2020-11-28 07:15

I was trying send a mail using smtp.gmail.com in django 1.8

My settings.py contains:

E         


        
相关标签:
7条回答
  • 2020-11-28 07:38

    I used this for django 1.11

    In settings.py

    EMAIL_USE_TLS = True
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_HOST_PASSWORD = 'sender' #sender mail password
    EMAIL_HOST_USER = 'sender@mail.com' #sender mail username
    EMAIL_PORT = 587
    DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
    

    In view.py

    send_mail('mail subject', 'body content',settings.EMAIL_HOST_USER,
                          ['receiver@mail.com'], fail_silently=False)
    

    and goto https://myaccount.google.com/u/0/security?hl=en to enable Less secure app access

    0 讨论(0)
  • 2020-11-28 07:45

    This works for me:

    settings.py

    EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_HOST_PASSWORD = 'test'
    EMAIL_HOST_USER = 'test@gmail.com'
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True
    

    Unlock Captcha: https://accounts.google.com/DisplayUnlockCaptcha

    views.py

    email = EmailMessage(
        'subject_message',
        'content_message',
        'sender smtp gmail' +'<sender@gmail.com>',
        ['receiver@gmail.com'],
        headers = {'Reply-To': 'contact_email@gmail.com' }
    )
    email.send()
    
    0 讨论(0)
  • 2020-11-28 07:47

    I tested this and worked perfect in django 1.8:
    first you should check this link, provided by google which you did :)
    notice that for some strange reasons that I don't know,you have to code like this in view.py or shell:

    import django
    from django.conf import settings
    from django.core.mail import send_mail
    
    send_mail('Subject here', 'Here is the message.', settings.EMAIL_HOST_USER,
             ['to@example.com'], fail_silently=False)
    

    also this is my settings in setting.py file:

    EMAIL_USE_TLS = True
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.gmail.com'
    EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
    EMAIL_HOST_USER = 'xxxx@gmail.com' #my gmail username
    EMAIL_PORT = 587
    DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
    
    0 讨论(0)
  • 2020-11-28 07:50

    for me in settings.py:

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

    and views.py:

    from django.core.mail import EmailMessage
    
    email = EmailMessage('title', 'body', to=[email])
    email.send()
        
    

    and: https://accounts.google.com/DisplayUnlockCaptcha

    and also make sure you turn on permission for less secure apps.

    0 讨论(0)
  • 2020-11-28 07:53

    Remember to:

    Go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable this option.

    About this option: https://support.google.com/accounts/answer/6010255

    0 讨论(0)
  • 2020-11-28 07:53

    In settings.py change this

    EMAIL_HOST='imap.gmail.com'
    EMAIL_PORT = 587
    EMAIL_HOST_USER = 'yadavabhishek260@gmail.com'
    EMAIL_HOST_PASSWORD ='**********'
    EMAIL_USE_SSL=False
    EMAIL_USE_TLS= True
    
    0 讨论(0)
提交回复
热议问题