django-dynamic-smtp 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ /venv/
2
+ /build/
3
+ /db.sqlite3
4
+ __pycache__
@@ -0,0 +1,18 @@
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v3.2.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ - id: end-of-file-fixer
7
+ - id: check-yaml
8
+ - id: check-added-large-files
9
+ - repo: https://github.com/psf/black
10
+ rev: 22.10.0
11
+ hooks:
12
+ - id: black
13
+ - repo: https://github.com/astral-sh/ruff-pre-commit
14
+ rev: v0.9.10
15
+ hooks:
16
+ - id: ruff
17
+ args: [ --fix ]
18
+ - id: ruff-format
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 João Seckler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,50 @@
1
+ Metadata-Version: 2.4
2
+ Name: django-dynamic-smtp
3
+ Version: 0.0.1
4
+ Summary: Configure email settings in the admin
5
+ Project-URL: Homepage, https://gitlab.com/joaoseckler/django-dynamic-smtp
6
+ Project-URL: Issues, https://gitlab.com/joaoseckler/django-dynamic-smtp/issues
7
+ Author-email: João Seckler <jseckler@riseup.net>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.7
13
+ Description-Content-Type: text/markdown
14
+
15
+ # Django dynamic SMTP
16
+
17
+ Configure email configuration in the admin.
18
+
19
+ ## Usage
20
+
21
+ 1. Install with `pip install django-dynamic-smtp`
22
+
23
+ 2. Add `dynamic_smtp` to `INSTALLED_APPS`:
24
+
25
+ ```python
26
+ INSTALLED_APPS = [
27
+ ...,
28
+ "dynamic_smtp",
29
+ ]
30
+ ```
31
+
32
+ 3. Configure your email backend:
33
+
34
+ ```
35
+ EMAIL_BACKEND = (
36
+ "django.core.mail.backends.console.EmailBackend"
37
+ if DEBUG
38
+ else "dynamic_smtp.email.DynamicSMPTEmailBackend"
39
+ )
40
+ ```
41
+
42
+ 4. Migrate: `./manage.py migrate`
43
+ 5. Access you admin and configure your SMTP settings
44
+
45
+ ## Contributing
46
+
47
+ All contributions are welcome! To setup you environment:
48
+
49
+ 1. `pip install -r dev.requirements.txt`
50
+ 2. `pre-commit install`
@@ -0,0 +1,36 @@
1
+ # Django dynamic SMTP
2
+
3
+ Configure email configuration in the admin.
4
+
5
+ ## Usage
6
+
7
+ 1. Install with `pip install django-dynamic-smtp`
8
+
9
+ 2. Add `dynamic_smtp` to `INSTALLED_APPS`:
10
+
11
+ ```python
12
+ INSTALLED_APPS = [
13
+ ...,
14
+ "dynamic_smtp",
15
+ ]
16
+ ```
17
+
18
+ 3. Configure your email backend:
19
+
20
+ ```
21
+ EMAIL_BACKEND = (
22
+ "django.core.mail.backends.console.EmailBackend"
23
+ if DEBUG
24
+ else "dynamic_smtp.email.DynamicSMPTEmailBackend"
25
+ )
26
+ ```
27
+
28
+ 4. Migrate: `./manage.py migrate`
29
+ 5. Access you admin and configure your SMTP settings
30
+
31
+ ## Contributing
32
+
33
+ All contributions are welcome! To setup you environment:
34
+
35
+ 1. `pip install -r dev.requirements.txt`
36
+ 2. `pre-commit install`
@@ -0,0 +1,4 @@
1
+ django-stubs==5.1.3
2
+ django-stubs-ext==5.1.3
3
+ types-PyYAML==6.0.12.20241230
4
+ typing_extensions==4.12.2
File without changes
@@ -0,0 +1,56 @@
1
+ from smtplib import SMTPException
2
+
3
+ from django.contrib import admin, messages
4
+ from django.core import mail
5
+ from django.core.mail.backends.console import EmailBackend as ConsoleEmailBackend
6
+ from django.http import HttpRequest
7
+ from django.utils.translation import gettext_lazy as _
8
+ from django_object_actions import DjangoObjectActions, action
9
+ from solo.admin import SingletonModelAdmin
10
+
11
+ from .models import EmailConfiguration
12
+
13
+
14
+ @admin.register(EmailConfiguration)
15
+ class EmailConfigurationAdmin(DjangoObjectActions, SingletonModelAdmin):
16
+ change_actions = ["test_email"]
17
+
18
+ @action(
19
+ label=_("Test email"),
20
+ description=_(
21
+ "Send a test email from the account configured below to the "
22
+ "email registered as sender. Save before using."
23
+ ),
24
+ )
25
+ def test_email(self, request: HttpRequest, obj: EmailConfiguration):
26
+ host = request.get_host()
27
+ try:
28
+ with mail.get_connection() as connection:
29
+ if type(connection) is ConsoleEmailBackend:
30
+ raise AssertionError(
31
+ _("Using debug backend. Try using DEBUG=False in settings")
32
+ )
33
+ __ = mail.send_mail(
34
+ message=_(
35
+ "This email has been sent to verify if the "
36
+ "mailing system is well configured in the "
37
+ "%(host)s's administration. It seems so!"
38
+ )
39
+ % {"host": host},
40
+ from_email=obj.from_email,
41
+ subject=_("[%(host)s] Email test") % {"host": host},
42
+ recipient_list=[obj.from_email],
43
+ fail_silently=False,
44
+ connection=connection,
45
+ )
46
+ except (SMTPException, AssertionError) as e:
47
+ messages.error(
48
+ request,
49
+ _("Email not well configured: %s") % str(e),
50
+ )
51
+ return
52
+
53
+ messages.success(
54
+ request,
55
+ _("Email is well configured! :)"),
56
+ )
@@ -0,0 +1,8 @@
1
+ from django.apps import AppConfig
2
+ from django.utils.translation import gettext_lazy as _
3
+
4
+
5
+ class DynamicSmtpConfig(AppConfig):
6
+ default_auto_field = "django.db.models.BigAutoField"
7
+ name = "dynamic_smtp"
8
+ verbose_name = _("Email configuration")
@@ -0,0 +1,65 @@
1
+ from collections.abc import Sequence
2
+
3
+ from bs4 import BeautifulSoup
4
+ from django.core import mail
5
+ from django.core.mail.backends.smtp import EmailBackend
6
+
7
+ from .models import EmailConfiguration
8
+
9
+
10
+ class DynamicSMPTEmailBackend(EmailBackend):
11
+ def __init__(
12
+ self,
13
+ host: str | None = None,
14
+ port: str | None = None,
15
+ username: str | None = None,
16
+ password: str | None = None,
17
+ use_tls: str | None = None,
18
+ fail_silently: bool = False,
19
+ use_ssl: str | None = None,
20
+ timeout: str | None = None,
21
+ ssl_keyfile: str | None = None,
22
+ ssl_certfile: str | None = None,
23
+ **kwargs: ...,
24
+ ):
25
+ conf = EmailConfiguration.get_solo()
26
+
27
+ super().__init__(
28
+ host=host or conf.host,
29
+ port=port or conf.port,
30
+ username=username or conf.username,
31
+ password=password or conf.password,
32
+ use_tls=use_tls or conf.use_tls,
33
+ fail_silently=fail_silently,
34
+ use_ssl=use_ssl or conf.use_ssl,
35
+ timeout=timeout or conf.timeout,
36
+ ssl_keyfile=ssl_keyfile,
37
+ ssl_certfile=ssl_certfile,
38
+ **kwargs,
39
+ )
40
+
41
+
42
+ def html2text(html: str):
43
+ return BeautifulSoup(html, "lxml").get_text()
44
+
45
+
46
+ def send_mail(
47
+ subject: str,
48
+ message: str,
49
+ recipients: Sequence[str] | str,
50
+ ):
51
+ mailconf = EmailConfiguration.get_solo()
52
+ recipient_list = (
53
+ recipients.split(",") if isinstance(recipients, str) else recipients
54
+ )
55
+
56
+ return mail.send_mail(
57
+ subject,
58
+ html2text(message),
59
+ recipient_list=recipient_list,
60
+ html_message=message,
61
+ fail_silently=False,
62
+ auth_user=mailconf.username,
63
+ auth_password=mailconf.password,
64
+ from_email=mailconf.from_email,
65
+ )
@@ -0,0 +1,99 @@
1
+ # Generated by Django 5.1.7 on 2025-03-12 16:24
2
+
3
+ import tinymce.models
4
+ from django.db import migrations, models
5
+
6
+
7
+ class Migration(migrations.Migration):
8
+ initial = True
9
+
10
+ dependencies = []
11
+
12
+ operations = [
13
+ migrations.CreateModel(
14
+ name="EmailConfiguration",
15
+ fields=[
16
+ (
17
+ "id",
18
+ models.BigAutoField(
19
+ auto_created=True,
20
+ primary_key=True,
21
+ serialize=False,
22
+ verbose_name="ID",
23
+ ),
24
+ ),
25
+ (
26
+ "activated",
27
+ models.BooleanField(
28
+ default=False,
29
+ help_text="Só ative essa opção depois de testar as configurações de email no botão ao lado. Quando ativado, as notificações vão ser enviadas por email e será possível a usuários redefinir suas senhas.",
30
+ verbose_name="Email está ativado?",
31
+ ),
32
+ ),
33
+ (
34
+ "host",
35
+ models.CharField(
36
+ default="smtp.gmail.com", max_length=128, verbose_name="Host"
37
+ ),
38
+ ),
39
+ (
40
+ "port",
41
+ models.PositiveIntegerField(default=587, verbose_name="Porta"),
42
+ ),
43
+ (
44
+ "username",
45
+ models.CharField(
46
+ default="example@gmail.com",
47
+ max_length=128,
48
+ verbose_name="Usuário",
49
+ ),
50
+ ),
51
+ (
52
+ "password",
53
+ models.CharField(
54
+ default="1234", max_length=128, verbose_name="Senha"
55
+ ),
56
+ ),
57
+ ("use_tls", models.BooleanField(default=True, verbose_name="Usar TLS")),
58
+ (
59
+ "use_ssl",
60
+ models.BooleanField(default=False, verbose_name="Usar SSL"),
61
+ ),
62
+ (
63
+ "timeout",
64
+ models.IntegerField(
65
+ blank=True, default=120, null=True, verbose_name="Timeout"
66
+ ),
67
+ ),
68
+ (
69
+ "from_name",
70
+ models.CharField(
71
+ blank=True,
72
+ max_length=255,
73
+ null=True,
74
+ verbose_name="Nome do remetente",
75
+ ),
76
+ ),
77
+ (
78
+ "from_email",
79
+ models.EmailField(
80
+ default="admin@example.com",
81
+ max_length=254,
82
+ verbose_name="Email remetente",
83
+ ),
84
+ ),
85
+ (
86
+ "signature",
87
+ tinymce.models.HTMLField(
88
+ blank=True,
89
+ help_text="Para usar essa assinatura em seus emails, utilize a variável <code>{{ signature }}</code>.",
90
+ null=True,
91
+ verbose_name="Assinatura dos emails",
92
+ ),
93
+ ),
94
+ ],
95
+ options={
96
+ "verbose_name": "Email configuration",
97
+ },
98
+ ),
99
+ ]
@@ -0,0 +1,81 @@
1
+ from typing import override
2
+
3
+ from django.db import models
4
+ from django.utils.html import format_html
5
+ from django.utils.translation import gettext_lazy as _
6
+ from solo.models import SingletonModel
7
+ from tinymce.models import HTMLField
8
+
9
+
10
+ class EmailConfiguration(SingletonModel):
11
+ activated = models.BooleanField(
12
+ default=False,
13
+ verbose_name=_("Email está ativado?"),
14
+ help_text=_(
15
+ "Só ative essa opção depois de testar as configurações de "
16
+ + "email no botão ao lado. Quando ativado, as notificações vão "
17
+ + "ser enviadas por email e será possível a usuários "
18
+ + "redefinir suas senhas."
19
+ ),
20
+ )
21
+ host = models.CharField(
22
+ max_length=128,
23
+ verbose_name=_("Host"),
24
+ default="smtp.gmail.com",
25
+ )
26
+ port = models.PositiveIntegerField(
27
+ verbose_name=_("Porta"),
28
+ default=587,
29
+ )
30
+ username = models.CharField(
31
+ max_length=128,
32
+ verbose_name=_("Usuário"),
33
+ default="example@gmail.com",
34
+ )
35
+ password = models.CharField(
36
+ max_length=128,
37
+ verbose_name=_("Senha"),
38
+ default="1234",
39
+ )
40
+ use_tls = models.BooleanField(
41
+ default=True,
42
+ verbose_name=_("Usar TLS"),
43
+ )
44
+ use_ssl = models.BooleanField(
45
+ default=False,
46
+ verbose_name=_("Usar SSL"),
47
+ )
48
+ timeout = models.IntegerField(
49
+ blank=True,
50
+ null=True,
51
+ default=120,
52
+ verbose_name=_("Timeout"),
53
+ )
54
+ from_name = models.CharField(
55
+ max_length=255,
56
+ verbose_name=_("Nome do remetente"),
57
+ null=True,
58
+ blank=True,
59
+ )
60
+ from_email = models.EmailField(
61
+ verbose_name=_("Email remetente"),
62
+ default="admin@example.com",
63
+ )
64
+ signature = HTMLField(
65
+ blank=True,
66
+ null=True,
67
+ verbose_name=_("Assinatura dos emails"),
68
+ help_text=format_html(
69
+ "{}<code>{}</code>{}",
70
+ _("Para usar essa assinatura em seus emails, utilize a variável "),
71
+ "{{ signature }}",
72
+ ".",
73
+ ),
74
+ )
75
+
76
+ @override
77
+ def __str__(self):
78
+ return str(self._meta.verbose_name)
79
+
80
+ class Meta:
81
+ verbose_name = _("Email configuration")
@@ -0,0 +1,32 @@
1
+ from pathlib import Path
2
+
3
+ from django import setup
4
+ from django.conf import settings
5
+ from django.core.management import call_command
6
+
7
+ BASE_DIR = Path(__file__).resolve().parent
8
+
9
+
10
+ def boot_django():
11
+ settings.configure(
12
+ BASE_DIR=BASE_DIR,
13
+ DEBUG=True,
14
+ DATABASES={
15
+ "default": {
16
+ "ENGINE": "django.db.backends.sqlite3",
17
+ "NAME": BASE_DIR / "db.sqlite3",
18
+ }
19
+ },
20
+ INSTALLED_APPS=(
21
+ "django.contrib.auth",
22
+ "django.contrib.contenttypes",
23
+ "dynamic_smtp",
24
+ ),
25
+ )
26
+
27
+ setup()
28
+
29
+
30
+ boot_django()
31
+ ret = call_command("makemigrations", "dynamic_smtp")
32
+ print(ret)
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "django-dynamic-smtp"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name="João Seckler", email="jseckler@riseup.net" },
10
+ ]
11
+ description = "Configure email settings in the admin"
12
+ readme = "README.md"
13
+ requires-python = ">=3.7"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ ]
18
+ license = "MIT"
19
+ license-files = ["LICEN[CS]E*"]
20
+
21
+ [project.urls]
22
+ Homepage = "https://gitlab.com/joaoseckler/django-dynamic-smtp"
23
+ Issues = "https://gitlab.com/joaoseckler/django-dynamic-smtp/issues"
24
+
25
+ [tool.hatch.build.targets.wheel]
26
+ packages = ["dynamic_smtp"]
@@ -0,0 +1,10 @@
1
+ {
2
+ "include": [
3
+ "./dynamic_smtp/"
4
+ ],
5
+ "reportAny": false,
6
+ "reportImplicitStringConcatenation": false,
7
+ "reportUnannotatedClassAttribute": false,
8
+ "reportIgnoreCommentWithoutRule": false,
9
+ "enableTypeIgnoreComments": true
10
+ }
@@ -0,0 +1,9 @@
1
+ asgiref==3.8.1
2
+ beautifulsoup4==4.13.3
3
+ Django==5.1.7
4
+ django-object-actions==4.3.0
5
+ django-solo==2.4.0
6
+ django-tinymce==4.1.0
7
+ lxml==5.3.1
8
+ soupsieve==2.6
9
+ sqlparse==0.5.3