Skip to content

Commit

Permalink
Add spearate email name in email configuration #81
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean28518 committed Apr 19, 2024
1 parent ef42244 commit 53eeb7f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cfg.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export LINUX_ARBEITSPLATZ_CONFIGURED=False
# (This is only for the management interface, not for other services like nextcloud)
export EMAIL_HOST=""
export EMAIL_PORT=""
export EMAIL_HOST_USER=""
export EMAIL_HOST_USER=""
export EMAIL_HOST_EMAIL=""
export EMAIL_HOST_PASSWORD=""

# only activate (uncomment) one of these:
Expand Down
3 changes: 2 additions & 1 deletion src/lac/lac/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@
if not _email_port_string.isnumeric():
_email_port_string = "465"
EMAIL_PORT = int(_email_port_string) # <- smtp port [e.g. 587]
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER") # <- username
EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER") # <- username usually the same as email
EMAIL_HOST_EMAIL = os.getenv("EMAIL_HOST_EMAIL") # <- email
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD") # <- password
EMAIL_USE_TLS = os.getenv("EMAIL_USE_TLS") == "True"
EMAIL_USE_SSL = os.getenv("EMAIL_USE_SSL") == "True"
Expand Down
7 changes: 3 additions & 4 deletions src/lac/unix/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# If this one returns None the mail was sent successfully
def send_mail(recipient, subject, message, attachment_path=""):
email = EmailMessage(subject=subject, body=message, from_email=settings.EMAIL_HOST_USER, to=[recipient])
email = EmailMessage(subject=subject, body=message, from_email=settings.EMAIL_HOST_EMAIL, to=[recipient])
# Check also if attachment_path exists
if attachment_path != "" and attachment_path is not None and os.path.exists(attachment_path):
email.attach_file(attachment_path)
Expand All @@ -18,6 +18,5 @@ def send_mail(recipient, subject, message, attachment_path=""):


def are_mail_settings_configured():
if settings.EMAIL_HOST_USER == "" or settings.EMAIL_HOST_PASSWORD == "" or settings.EMAIL_HOST == "" or settings.EMAIL_PORT == "":
return False
return True
return settings.EMAIL_HOST_USER and settings.EMAIL_HOST_EMAIL and settings.EMAIL_HOST_PASSWORD and settings.EMAIL_HOST and settings.EMAIL_PORT

1 change: 1 addition & 0 deletions src/lac/unix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class EmailConfiguration(forms.Form):
server = forms.CharField(label="E-Mail Server", max_length=100, widget=forms.TextInput(attrs={"placeholder": "mail.example.com"}))
port = forms.IntegerField(label="E-Mail Port", min_value=0, max_value=10000, widget=forms.NumberInput(attrs={"placeholder": "587"}))
user = forms.CharField(label="E-Mail Benutzername", max_length=100, widget=forms.TextInput(attrs={"placeholder": "example@example.com"}))
email = forms.EmailField(label="E-Mail Adresse", max_length=100, widget=forms.EmailInput(attrs={"placeholder": "example@example.com"}))
password = forms.CharField(label="E-Mail Passwort", max_length=100, widget=forms.PasswordInput(attrs={"placeholder": "Passwort"}), required=False)
encryption = forms.ChoiceField(label="E-Mail Verschlüsselung", choices=[("TLS", "TLS"), ("SSL", "SSL")], widget=forms.Select(attrs={"class": "form-control"}))

Expand Down
5 changes: 3 additions & 2 deletions src/lac/unix/unix_scripts/general/update_email_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def update_email_settings(email_settings):
settings.EMAIL_HOST = email_settings["server"]
settings.EMAIL_PORT = email_settings["port"]
settings.EMAIL_HOST_USER = email_settings["user"]
settings.EMAIL_HOST_EMAIL = email_settings["email"]
settings.EMAIL_HOST_PASSWORD = email_settings["password"]
if email_settings["encryption"] == "TLS":
settings.EMAIL_USE_TLS = "True"
Expand All @@ -26,8 +27,8 @@ def update_email_settings(email_settings):

# Change email settings in nextcloud if nextcloud is installed
if unix.is_nextcloud_available():
from_adress = email_settings["user"].split("@")[0]
mail_domain = email_settings["user"].split("@")[1]
from_adress = email_settings["email"].split("@")[0]
mail_domain = email_settings["email"].split("@")[1]
os.system(f'sudo -u www-data php {settings.NEXTCLOUD_INSTALLATION_DIRECTORY}/occ config:system:set mail_smtpauthtype --value="LOGIN"')
os.system(f'sudo -u www-data php {settings.NEXTCLOUD_INSTALLATION_DIRECTORY}/occ config:system:set mail_smtpmode --value="smtp"')
os.system(f'sudo -u www-data php {settings.NEXTCLOUD_INSTALLATION_DIRECTORY}/occ config:system:set mail_smtpsecure --value="{email_settings["encryption"]}"')
Expand Down
2 changes: 2 additions & 0 deletions src/lac/unix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def email_configuration(request):
"server": cfg.get_value("EMAIL_HOST", ""),
"port": cfg.get_value("EMAIL_PORT", ""),
"user": cfg.get_value("EMAIL_HOST_USER", ""),
"email": cfg.get_value("EMAIL_HOST_EMAIL", ""),
"password": cfg.get_value("EMAIL_HOST_PASSWORD", ""),
}
if cfg.get_value("EMAIL_USE_TLS", "False") == "True":
Expand All @@ -323,6 +324,7 @@ def email_configuration(request):
cfg.set_value("EMAIL_HOST", form.cleaned_data["server"])
cfg.set_value("EMAIL_PORT", form.cleaned_data["port"])
cfg.set_value("EMAIL_HOST_USER", form.cleaned_data["user"])
cfg.set_value("EMAIL_HOST_EMAIL", form.cleaned_data["email"])
if form.cleaned_data["password"] != "":
# We need to escape the $ sign because of the bash syntax in our config file
cfg.set_value("EMAIL_HOST_PASSWORD", form.cleaned_data["password"].replace("$", "\$"))
Expand Down

0 comments on commit 53eeb7f

Please sign in to comment.