summaryrefslogtreecommitdiff
path: root/fossee_manim/send_mails.py
blob: a329e8b4cee3134fb3a9f2e6e8bb92d25a362920 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from django.core.mail import EmailMultiAlternatives, send_mail
from django.conf import settings
from django.utils.crypto import get_random_string
from os import listdir, path
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from time import sleep
from textwrap import dedent
from random import randint
from smtplib import SMTP
from string import punctuation, digits
from hashlib import sha256
import logging.config
import re
try:
	from string import letters
except ImportError:
	from string import ascii_letters as letters
from fossee_anime.settings import (
					EMAIL_HOST,
					EMAIL_PORT,
					EMAIL_HOST_USER,
					EMAIL_HOST_PASSWORD,
					EMAIL_USE_TLS,
					PRODUCTION_URL,
					SENDER_EMAIL,
					ADMIN_EMAIL
					)


__author__ = "Akshen Doke"


def validateEmail(email):
	if len(email) > 7:
		if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$",
					email) is not None:
			return 1
		return 0


def generate_activation_key(username):
	"""Generates hashed secret key for email activation"""
	chars = letters + digits + punctuation
	secret_key = get_random_string(randint(10, 40), chars)
	return sha256((secret_key + username).encode('utf-8')).hexdigest()


def send_email(request, call_on, contributor=None, key=None, proposal=None):
	'''
	'''

	try:
		with open(path.join(settings.LOG_FOLDER,
				  'emailconfig.yaml'), 'r') as configfile:
			config_dict = yaml.load(configfile)
		logging.config.dictConfig(config_dict)
	except:
		print('File Not Found and Configuration Error')

	if call_on == "Registration":
		message = dedent("""\
					Thank you for registering as a contributor with us.
					Please click on the below link to
					activate your account
					{0}/activate_user/{1}
					After activation you can proceed to submit proposals for
					animations.
					In case of queries regarding submition of proposal(s),
					revert to this email.""".format(PRODUCTION_URL, key))

		logging.info("New Registration from: %s", request.user.email)
		try:
			send_mail(
				"User/Contributor Registration at FOSSEE, IIT Bombay", message,
				SENDER_EMAIL, [request.user.email], fail_silently=True)

		except Exception:
			send_smtp_email(request=request,
				subject="User/Contributor Registration - FOSSEE, IIT Bombay",
				message=message, other_email=request.user.email,
				)
	elif call_on == 'released':
		message = dedent("""\
					Hey {0},

					Congratulations! your animations has been released on
					FOSSEE's website.
					Your animation will be live in 72 working hours.
					Please start with your honorarium process

					In case of queries, please revert to this
					email.""".format(contributor.profile.user.username))

		logging.info("Released Animation: %s", request.user.email)
		send_mail(
			"Congratulations! Animation Released!", message, SENDER_EMAIL,
				[contributor.profile.user.email], fail_silently=True
				)
	elif call_on == 'rejected':
		message = dedent("""\
					Hey {0},

					We are sorry to inform you that your proposal for
					FOSSEE Animation is rejected.
					You can work on the feedback given by the reviewer or
					send us another proposal on a different topic!

					In case of queries, please revert to this
					email.""".format(contributor.profile.user.username))

		logging.info("Animation Rejected: %s", request.user.email)
		send_mail(
			"FOSSEE Animation Status Update", message, SENDER_EMAIL,
			[contributor.profile.user.email], fail_silently=True
				)
	elif call_on == 'changes':
		message = dedent("""\
					Hey {0},

					Please check your proposal {1}
					for comments by our reviewers
					
					In case of queries, please revert to this
					email.""".format(contributor.profile.user.username,
								    proposal.title))

		logging.info("Changes Required: %s", request.user.email)
		send_mail(
			"FOSSEE Animation Changes required", message, SENDER_EMAIL,
			[contributor.profile.user.email], fail_silently=True)