diff --git a/.env.default b/.env.default index c2d83f84852c78ae82ca425ccc7cc24968798861..2bdc9b50ec713212ded371f4f84388c5dd35b2ec 100644 --- a/.env.default +++ b/.env.default @@ -16,6 +16,13 @@ BUILDING_FOOTPRINT_DB=$BUILDING_FOOTPRINT_DB UTILITY_BILL_DB=$UTILITY_BILL_DB CORS_ORIGIN_WHITELIST=$CORS_ORIGIN_WHITELIST BLOCPOWER_PHONE_NUMBER=$BLOCPOWER_PHONE_NUMBER +EMAIL_HOST=$EMAIL_HOST +EMAIL_PORT=$EMAIL_PORT +EMAIL_USE_TLS=$EMAIL_USE_TLS +EMAIL_PROD_USERNAME=$EMAIL_PROD_USERNAME +EMAIL_PROD_PASSWORD=$EMAIL_PROD_PASSWORD +EMAIL_USERNAME=$EMAIL_USERNAME +EMAIL_PASSWORD=$EMAIL_PASSWORD TEST_EMAIL=$TEST_EMAIL PROD_EMAIL=$PROD_EMAIL FROM_EMAIL=$FROM_EMAIL diff --git a/bloclink/apps/bis/views.py b/bloclink/apps/bis/views.py index 737dc82e3ba842b9039ba29c0287ca74c200f777..f0eecd6315f8d84ad3ade9f5245b48e8133ab305 100644 --- a/bloclink/apps/bis/views.py +++ b/bloclink/apps/bis/views.py @@ -8,7 +8,8 @@ from django.views import View from .models import Questions, MapBoxBuilding, Answers, SubmittedSurvey, Users, Surveys, SurveyAreas, RecommendationSetMap, Ecm, Report from django.forms.models import model_to_dict from django.template.loader import render_to_string -from django.core.mail import EmailMessage +from django.core.mail import EmailMessage, get_connection +from django.db import connections import re import boto3 from bpeng.bis.report import generate_pns_report @@ -280,34 +281,47 @@ class SubmitQuestionnaire(View): resource = dest_ppt_file.split('/')[-1], last_updated = datetime.now() ).save(using='bis') - + + # Setting connections and from_email + if os.environ['ENVIRONMENT'] == 'prod': + connection = get_connection(host=settings.EMAIL_HOST, + port=settings.EMAIL_PORT, + username=settings.EMAIL_PROD_USERNAME, + password=settings.EMAIL_PROD_PASSWORD, + use_tls=settings.EMAIL_USE_TLS) + from_email = settings.EMAIL_PROD_USERNAME + else: + connection = get_connection(host=settings.EMAIL_HOST, + port=settings.EMAIL_PORT, + username=settings.EMAIL_USERNAME, + password=settings.EMAIL_PASSWORD, + use_tls=settings.EMAIL_USE_TLS) + from_email = settings.EMAIL_USERNAME + # Sending Emails firstName = put['firstName'] lastName = put['lastName'] - toEmail = put['email'] + customerEmail = put['email'] phone = put['phone'] - from_email = settings.FROM_EMAIL surveyId = put['surveyId'] if surveyId == 1: if put['buildingQualified'] == True: msg_html = render_to_string('congratulations_email.html', {'firstName': firstName, 'lastName': lastName, 'buildingAddress': buildingAddress, 'phone':settings.BLOCPOWER_PHONE_NUMBER}) - msg = EmailMessage('Congratulations', msg_html, from_email, [toEmail]) + msg = EmailMessage('Congratulations', msg_html, from_email, [customerEmail], connection=connection) msg.content_subtype = "html" msg.attach_file('bloclink/templates/welcome.pdf') msg.send() else: msg_html = render_to_string('thank_you_email.html', {'firstName': firstName, 'lastName': lastName, 'buildingAddress': buildingAddress, 'phone':settings.BLOCPOWER_PHONE_NUMBER}) - msg = EmailMessage('Thank you', msg_html, from_email, [toEmail]) + msg = EmailMessage('Thank you', msg_html, from_email, [customerEmail], connection=connection) msg.content_subtype = "html" msg.send() elif surveyId == 2 or surveyId == 3: msg_html = render_to_string('thank_you_email.html', {'firstName': firstName, 'lastName': lastName, 'buildingAddress': buildingAddress, 'phone':settings.BLOCPOWER_PHONE_NUMBER}) - msg = EmailMessage('Thank you', msg_html, from_email, [toEmail]) + msg = EmailMessage('Thank you', msg_html, from_email, [customerEmail], connection=connection) msg.content_subtype = "html" - # We are not sending PPT to the user rn. - # msg.attach_file(dest_ppt_file) msg.send() # Get all building related data @@ -327,7 +341,7 @@ class SubmitQuestionnaire(View): msg_txt = render_to_string('new_building.txt') msg_html = render_to_string('new_building.html', {'firstName': firstName, 'lastName': lastName, - 'email': toEmail, + 'email': customerEmail, 'contactNumber': phone, 'buildingId': put['buildingId'], 'address': buildingData['address'], @@ -335,19 +349,18 @@ class SubmitQuestionnaire(View): oakland_survey = 2 milwaukee_survey = 3 - subject = 'BIS - ' + buildingData['address'] - if os.environ['ENVIRONMENT'] == 'prod': - msg = EmailMessage(subject, msg_html, from_email, [settings.PROD_EMAIL]) - msg.content_subtype = "html" - if put['surveyId'] in [oakland_survey, milwaukee_survey]: - msg.attach_file(dest_ppt_file) - msg.send() - else: - msg = EmailMessage("TEST - " + subject, msg_html, from_email, [settings.TEST_EMAIL]) - msg.content_subtype = "html" - if put['surveyId'] in [oakland_survey, milwaukee_survey]: - msg.attach_file(dest_ppt_file) - msg.send() + + # Define a subject based on ENVIRONMENT + subject = 'BIS - ' + buildingData['address'] if os.environ['ENVIRONMENT'] == 'prod' else 'Test - BIS - ' + buildingData['address'] + + # Define an email_recipient based on ENVIRONMENT + email_recipient = settings.PROD_EMAIL if os.environ['ENVIRONMENT'] == 'prod' else settings.TEST_EMAIL + + msg = EmailMessage(subject, msg_html, from_email, [email_recipient], connection=connection) + msg.content_subtype = "html" + if put['surveyId'] in [oakland_survey, milwaukee_survey]: + msg.attach_file(dest_ppt_file) + msg.send() return JsonResponse({ 'success': True, diff --git a/bloclink/settings.py b/bloclink/settings.py index 2bcc2c9e6d009df9cd19ed8a6762f784f9d42f4a..0c0a547756d62043e0303213b7fcd87510304ea8 100644 --- a/bloclink/settings.py +++ b/bloclink/settings.py @@ -34,6 +34,13 @@ PENTAHO_PASSWORD = config('PENTAHO_PASSWORD') # Building Intake Survey URL BIS_URL = config('BIS_URL') BLOCPOWER_PHONE_NUMBER = config('BLOCPOWER_PHONE_NUMBER') +EMAIL_HOST = config('EMAIL_HOST') +EMAIL_PORT = config('EMAIL_PORT') +EMAIL_USE_TLS = config('EMAIL_USE_TLS') +EMAIL_PROD_USERNAME = config('EMAIL_PROD_USERNAME') +EMAIL_PROD_PASSWORD = config('EMAIL_PROD_PASSWORD') +EMAIL_USERNAME = config('EMAIL_USERNAME') +EMAIL_PASSWORD = config('EMAIL_PASSWORD') TEST_EMAIL = config('TEST_EMAIL') PROD_EMAIL = config('PROD_EMAIL') FROM_EMAIL = config('FROM_EMAIL') @@ -235,12 +242,3 @@ REST_FRAMEWORK = { 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] } - -EMAIL_HOST = 'smtp.gmail.com' -EMAIL_HOST_PASSWORD = 'zdjiztpyaymeyhdb' -EMAIL_HOST_USER = 'welcome@blocpower.io' -EMAIL_PORT = 587 -EMAIL_USE_TLS = True - -EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' -DEFAULT_FROM_EMAIL = 'welcome@blocpower.io'