Easily send emails with Gmail with Django

Introduction

Currently, I am making a completely private mBaaS-like one, and if it becomes an OSS for everyone, I thought that I should think about accounts etc. so I sent an email.

environment

Since PyAPNs only supports Python 2.x, I implemented it using Python 2 with the knowledge that it will be hit by some elementary school students. I think that it can be used as it is in Python 3.x, so if you feel like it, please

I'm still not compatible with Django 1.10. please forgive me

Preparation

Virtual environment is ready with Virtualenv

$ pip install django==1.9.1
$ django-admin.py startproject sampleproject
$ django-admin.py startapp send_mail

Source code

For the time being, set settings.py like this

sampleproject/settings.py


# coding: utf-8
・ ・ ・
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'send_mail', #add to
]
・ ・ ・
LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'
・ ・ ・
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'passowrd'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

sampleproject/urls.py


# coding: utf-8

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('send_mail.urls', namespace = 'send_mail')),
]

I want to route each application myself, so it looks like this.

send_mail/views.py


# coding: utf-8

from django.core.mail import EmailMessage
from django.http import HttpResponse

def index(request):
   EmailMessage(u'subject', u'Text', to = ['[email protected]', '[email protected]']).send()
   return HttpResponse('Send your register email')

Create ʻurls.pydirectly undersend_mail`

send_mail/urls.py


from django.conf.urls import url
from accounts import views

urlpatterns = [
    url(r'^$', views.index, name = 'index'),
]

The source code itself looks like this. with this

$ python manage.py migrate
$ python manage.py runserver

And then http://127.0.0.1:8000

If it is true, you can do it by accessing, but it is a bit confusing due to Google's Authenticate specification.

Google side settings

スクリーンショット_2016-10-01_午前2_51_07.png

  1. First, log in with your account!
  2. Press the blue button labeled Account
  3. A page like the one below will appear. Click Login and Security スクリーンショット_2016-10-01_午前2_53_06.png
  4. At the bottom, there is an item called __ Allow unsafe apps __, so if it is off by default, turn it on. It will be available when it looks like the image below スクリーンショット 2016-10-01 午前2.54.24.png

Let's actually try

In my case, I wanted to send an email for those who forgot their login password, so the content of the email looks like this.

スクリーンショット 2016-10-01 午前2.57.19.png

An outrage that takes a screenshot of a Twitter tweet and pastes it as it is

bonus

Randomly create a string to reset the password

Python2.x series


import random, string

print(''.join([random.choice(string.letters + string.digits) for i in xrange(10)]))

Python3.x series


import random, string

print(''.join([random.choice(string.ascii_letters + string.digits) for i in range(10)]))

It's that easy!

Recommended Posts

Easily send emails with Gmail with Django
Send email with Django
Send using Python with Gmail
Note: Send an email with Django
Automatically send emails with Amazon SES
Send email via gmail with Python 3.4.3.
Send newsletters all at once with Gmail
Delete all unnecessary Gmail emails with API
Internationalization with django
CRUD with Django
I tried to send a registration completion email from Gmail with django.
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Easily daemonized with Supervisor
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Getting Started with Django 1
File upload with django
Use LESS with Django
Pooling mechanize with Django
Send email with Python
Use MySQL with Django
Start today with Django
Send Gmail in Python
Getting Started with Django 2
[Python] Send gmail with python: Send one by one with multiple image files attached
[Python] Send an email from gmail with two-step verification set
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Send Japanese email with Python3
Get started with Django! ~ Tutorial ⑤ ~
Easily serverless with Python with chalice
Minimal website environment with django
Create an API with Django
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Easily write if-elif with lambda
Getting Started with Python Django (1)
Create a homepage with django
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Easily build CNN with Keras
Do AES encryption with DJango
Getting Started with Python Django (6)
Real-time web with Django Channels
Double submit suppression with Django
Django REST framework with Vue.js
Easily cProfile with a decorator
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django