Build email sending function in Django application
It has been a long time since Django changed the wind. Today I will work with you to build the Email sending function in Django application. Let's start now. Let's go
Install the project
First we need to create a django project, and you can do that by opening the terminal and typing the following command:
1 | $ django-admin startproject plc_email (project name you can customize) |
Now we move into the project folder we just created
1 | $ cd plc_email |
Create a new app
Next we create a new app with the following command:
1 | $ python manage.py startapp send_email |
With the above command we have created 1 send_email directory in the project directory
Edit the project settings
In this step, please open the settings.py file , then you set this information for your desired configuration
1 2 3 4 5 6 7 | EMAIL_USE_TLS = True EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = 'Your gmail email' EMAIL_HOST_PASSWORD = 'Your gmail password' DEFAULT_FROM_EMAIL = 'Your name' DEFAULT_TO_EMAIL = 'Your email' |
In the INSTALLED_APPS block you need to add the following line to use the app send_email :
1 | 'send_email.apps.SendEmailConfig' |
After adding, this block has the following content:
1 2 3 4 5 6 7 8 9 | INSTALLED_APPS = [ 'send_email.apps.SendEmailConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ] |
Next you set the static file path for the application
1 2 3 4 5 6 | BASE_DIR = os.path.dirname (os.path.abspath (__ file__)) STATIC_ROOT = 'staticfiles' STATIC_URL = '/ static /' STATICFILES_DIRS = ( os.path.join (BASE_DIR, 'static'), ) |
Create urls
Editing the url in the plc_email directory is like this:
1 2 3 4 5 6 7 | from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url (r '^ admin /', admin.site.urls), url (r '^', include ('send_email.urls')), ] |
Then you create more files urls.py in the send_email directory with the file content:
1 2 3 4 5 6 7 8 | from django.conf.urls import url from. import views urlpatterns = [ url (r '^ $', views.index, name = 'index'), url (r '^ success', views.success, name = 'success'), ] |
Add Views
Go to file views.py in send_email folder and edit to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from django.shortcuts import render from django.http import HttpResponse import django from django.conf import settings from django.core.mail import send_mail def index (request): return render (request, 'index.html') def success (request): email = request.POST.get ('email', '') data = "" " Hello! I am Phuc. "" " send_mail ('Welcome!', data, "PLC", [email], fail_silently = False) return render (request, 'success.html') |
Add templates
Now we create the templates folder in the send_email folder . Then create 2 index.html and success.html files in the newly created templates directory . Content of index.html file :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | {% load staticfiles%} <! DOCTYPE html> <html> <head> <! - Import Google Icon Font -> <link href = "http://fonts.googleapis.com/icon?family=Material+Icons" rel = "stylesheet"> <link type = "text / css" rel = "stylesheet" href = "{% static 'css / style.css'%}" media = "screen, projection" /> </head> <body> <nav> <div class = "nav-wrapper container"> <a href="{% url'index' %}" class="brand-logo"> Django </a> </div> </nav> <div class = "login-f container"> <div class = "row"> <form class = "col s12 m6 offset-m3" action = "/ success" id = "form" method = "POST"> {% csrf_token%} <div class = "card blue-gray darken-1"> <div class = "card-content white-text"> <span class = "card-title"> Signup for Newsletter </span> <div class = "input-field col s12"> <input id = "email" type = "email" name = "email" class = "validate"> <label for = "email"> Email </label> </div> <div class = "col s12"> <input type = "checkbox" class = "filled-in" name = "checkbox" id = "test5" /> <label for = "test5"> Welcome Email </label> </div> </div> <div class = "card-action"> <button type = "submit"> SignUp </button> </div> </div> </form> </div> </div> <footer class = "page-footer"> <div class = "footer-copyright"> <div class = "container"> © 2016 Copyright PLC </div> </div> </footer> </body> </html> |
Content file success.html :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | {% load staticfiles%} <! DOCTYPE html> <html> <head> <! - Import Google Icon Font -> <link href = "http://fonts.googleapis.com/icon?family=Material+Icons" rel = "stylesheet"> <link type = "text / css" rel = "stylesheet" href = "{% static 'css / style.css'%}" media = "screen, projection" /> <! - Let browser know website is optimized for mobile -> <meta name = "viewport" content = "width = device-width, initial-scale = 1.0" /> </head> <body> <nav> <div class = "nav-wrapper container"> <a href="{% url'index' %}" class="brand-logo"> Django </a> </div> </nav> <div class = "login-f container"> <div class = "row"> <div class = "col s12 m6 offset-m3"> <div class = "card-panel teal"> <span class = "white-text"> Thank you very much! We have sent you a welcome email. <br> <br> PLC </span> </div> </div> </div> </div> <footer class = "page-footer"> <div class = "footer-copyright"> <div class = "container"> © 2016 Copyright PLC </div> </div> </footer> </body> </html> |
Let's try it!
Let's try it now
1 2 3 | $ python manage.py makemigrations $ python manage.py migrate $ python manage.py runserver |
So we've built the email sending function in the Django application. See you again on the blog post.
Happy coding!