Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task: email configuration settings #90

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions uipa_org/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@

rec = lambda x: re.compile(x, re.I | re.U)

# Helper functions for reading environment variables

# env() returns the value of an environment variable, or None if it is not set
def env(key, default=None):
return os.environ.get(key, default)

# required_env() returns the value of an environment variable, or raises an exception if it is not set
def required_env(key):
value = env(key)
if value is None or value == "":
raise ValueError(f"Required environment variable {key} is not set")
return value

THEME_ROOT = Path(__file__).resolve().parent.parent

# Note: The settings in this file are either Django settings or Froide settings.
Expand Down
45 changes: 44 additions & 1 deletion uipa_org/settings/development.py
Copy link
Member

@russtoku russtoku Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to define a new class, say, EmailTest, to use for testing sending email via a real SMTP server. This would isolate the configuration from the normal development workflow and prevent confusion about its use.

The current setting has the EMAIL_BACKEND set to django.core.mail.backends.console.EmailBackend which will print out any email being sent rather than sending it to an SMTP server.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from .base import UipaOrgThemeBase, env
from .base import UipaOrgThemeBase, env, required_env

class Dev(UipaOrgThemeBase):
DEBUG = True
Expand All @@ -13,3 +13,46 @@ def TEMPLATES(self):
TEMP = super().TEMPLATES
TEMP[0]["OPTIONS"]["debug"] = True
return TEMP

# Start of email settings
# Read the docs about these settings here: https://github.com/okfde/froide/blob/2dc1899cfe732c3f1d658f07ca86626dd5baa1a3/docs/configuration.rst#settings-for-sending-e-mail
# The exact settings can be seen https://github.com/okfde/froide/blob/2dc1899cfe732c3f1d658f07ca86626dd5baa1a3/froide/settings.py#L618

# General email settings
# This is the feature flag for enabling email sending & receiving
# This should be done with caution as it can lead to sending out emails to real public bodies
# It is recommended to create a test public body for testing purposes, so that real public bodies are not contacted
ENABLE_EMAIL = env("ENABLE_EMAIL", False)

if ENABLE_EMAIL:
SERVER_EMAIL = required_env("SERVER_EMAIL") # This is the inbound address for the SMTP server (i.e. [email protected])
DEFAULT_FROM_EMAIL = env(
"DEFAULT_FROM_EMAIL", "TEST UIPA.org <[email protected]>"
)
EMAIL_SUBJECT_PREFIX = env("EMAIL_SUBJECT_PREFIX", "[TEST UIPA.org] ")
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # This will be different for production

# These settings are for sending regular email notifications (not FOI requests to public bodies)
EMAIL_HOST = required_env("EMAIL_HOST") # This is the SMTP server for sending mail (i.e. smtp.postmarkapp.com)
EMAIL_PORT = env("EMAIL_PORT", 2525)
EMAIL_HOST_USER = required_env("EMAIL_HOST_USER") # This is the username for the SMTP server (i.e. 12345678-1234-1234-1234-123456789012) - this is the same as the password
EMAIL_HOST_PASSWORD = required_env("EMAIL_HOST_PASSWORD") # This is the password for the SMTP server (i.e. 12345678-1234-1234-1234-123456789012)
EMAIL_USE_TLS = env("EMAIL_USE_TLS", True)

# These settings are for sending FOI requests to public bodies
FOI_EMAIL_FIXED_FROM_ADDRESS = env("FOI_EMAIL_FIXED_FROM_ADDRESS", True)
FOI_EMAIL_HOST_USER = required_env("FOI_EMAIL_HOST_USER") # This is the username for the SMTP server (i.e. 12345678-1234-1234-1234-123456789012) - this is the same as the password
FOI_EMAIL_HOST_PASSWORD = required_env("FOI_EMAIL_HOST_PASSWORD") # This is the password for the SMTP server (i.e. 12345678-1234-1234-1234-123456789012)
FOI_EMAIL_HOST = required_env("FOI_EMAIL_HOST") # This is the SMTP server for sending mail (i.e. smtp.postmarkapp.com)
FOI_EMAIL_PORT = env("FOI_EMAIL_PORT", 2525)
FOI_EMAIL_USE_TLS = env("FOI_EMAIL_USE_TLS", True)

# These settings are for receiving FOI responses from public bodies
FOI_EMAIL_HOST_FROM = required_env("FOI_EMAIL_HOST_FROM") # This is the inbound address for the SMTP server (i.e. [email protected])
# When defining the "FOI_EMAIL_DOMAIN" setting for testing, Postmark enforces that all recipient addresses must share the same domain as the inbound address.
# Basically, if your email address ends in @example.com, then the "FOI_EMAIL_DOMAIN" setting must be set to "example.com".
FOI_EMAIL_DOMAIN = required_env("FOI_EMAIL_DOMAIN") # This defines the "From" address for the inbound email (i.e. beta-foi.uipa.org)
FOI_MAIL_SERVER_HOST = required_env("FOI_MAIL_SERVER_HOST") # This defines the "Message-Id" header for the inbound email (i.e. beta-foi.uipa.org)

# End of email settings