Skip to content

Commit 959d866

Browse files
authored
Merge pull request #40 from abhiram304/semail
Adding Send Email function
2 parents 0fd824b + e4c56aa commit 959d866

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ Django poll app is a full featured polling app. You have to register in this app
2929
<code>seeder.seed_all(30)</code>
3030
<p>Here 30 is a number of entry. You can use it as your own</p>
3131

32+
## Configure Email - Poll Owner receives Email when vote is cast by user
33+
34+
- Get your smtp host details and replace following values in your `settings.py`
35+
36+
```text
37+
# Configure email settings
38+
EMAIL_HOST = '<your smtp host>'
39+
EMAIL_PORT = '<smtp port>'
40+
EMAIL_HOST_USER = '<smtp host user>'
41+
EMAIL_HOST_PASSWORD = '<smtp host pass>'
42+
DEFAULT_FROM_EMAIL = '<from email address>'
43+
```
3244
<h2> Configuring OAuth login </h2>
3345
<details>
3446
<summary>Obtaining OAuth Client ID for Google</summary>

pollme/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,12 @@
143143

144144
SOCIAL_AUTH_URL_NAMESPACE = 'social'
145145
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/polls/list/user/'
146+
147+
# Configure email settings
148+
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
149+
EMAIL_HOST = 'smtp.gmail.com'
150+
EMAIL_PORT = 587
151+
EMAIL_USE_TLS = True
152+
EMAIL_HOST_USER = 'TBD'
153+
EMAIL_HOST_PASSWORD = 'TBD'
154+
DEFAULT_FROM_EMAIL = 'TBD' # The email address you want to send from

polls/views.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from .models import Poll, Choice, Vote
77
from .forms import PollAddForm, EditPollForm, ChoiceAddForm
88
from django.http import HttpResponse
9-
9+
from django.core.mail import send_mail
10+
from django.conf import settings # To access your email settings
1011

1112
@login_required()
1213
def polls_list(request):
@@ -51,7 +52,6 @@ def dashboard(request):
5152
context = {'poll_data': poll_data}
5253
return render(request, 'polls/dashboard.html', context)
5354

54-
5555
@login_required()
5656
def list_by_user(request):
5757
all_polls = Poll.objects.filter(owner=request.user)
@@ -211,8 +211,16 @@ def poll_vote(request, poll_id):
211211
if choice_id:
212212
choice = Choice.objects.get(id=choice_id)
213213
vote = Vote(user=request.user, poll=poll, choice=choice)
214-
vote.save()
215-
print(vote)
214+
vote.save()
215+
216+
# get poll creator email
217+
poll_creator_email = poll.owner.email
218+
# Construct the email
219+
subject = f'New vote for your poll: {poll.text}'
220+
message = f'A new vote has been cast for your poll "{poll.text}".'
221+
# Send the email
222+
send_mail(subject, message, settings.EMAIL_HOST_USER, [poll_creator_email])
223+
216224
return render(request, 'polls/poll_result.html', {'poll': poll})
217225
else:
218226
messages.error(

0 commit comments

Comments
 (0)