django-serversmtp 0.1.0__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.
- django_serversmtp-0.1.0/PKG-INFO +39 -0
- django_serversmtp-0.1.0/README.md +27 -0
- django_serversmtp-0.1.0/pyproject.toml +22 -0
- django_serversmtp-0.1.0/setup.cfg +4 -0
- django_serversmtp-0.1.0/src/django_serversmtp/__init__.py +6 -0
- django_serversmtp-0.1.0/src/django_serversmtp/backend.py +98 -0
- django_serversmtp-0.1.0/src/django_serversmtp.egg-info/PKG-INFO +39 -0
- django_serversmtp-0.1.0/src/django_serversmtp.egg-info/SOURCES.txt +10 -0
- django_serversmtp-0.1.0/src/django_serversmtp.egg-info/dependency_links.txt +1 -0
- django_serversmtp-0.1.0/src/django_serversmtp.egg-info/requires.txt +2 -0
- django_serversmtp-0.1.0/src/django_serversmtp.egg-info/top_level.txt +1 -0
- django_serversmtp-0.1.0/tests/test_backend.py +60 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-serversmtp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Django EmailBackend for the ServerSMTP HTTP API (POST /v1/send)
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Documentation, https://serversmtp.ro/docs#biblioteci
|
|
7
|
+
Keywords: django,email,serversmtp,transactional
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: serversmtp>=0.3.0
|
|
11
|
+
Requires-Dist: Django>=4.2
|
|
12
|
+
|
|
13
|
+
# django-serversmtp
|
|
14
|
+
|
|
15
|
+
EmailBackend oficial pentru **Django 4.2+** care trimite mesajele `django.core.mail` (`send_mail`, `EmailMessage`, `EmailMultiAlternatives`) prin API-ul HTTP **ServerSMTP** (`POST /v1/send`).
|
|
16
|
+
|
|
17
|
+
## Configurare (`settings.py`)
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
EMAIL_BACKEND = "django_serversmtp.ServerSMTPEmailBackend"
|
|
21
|
+
SERVERSMTP_API_KEY = os.environ["SERVERSMTP_API_KEY"]
|
|
22
|
+
DEFAULT_FROM_EMAIL = "Facturi <facturi@exemplu.invalid>"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Trimitere cu atașamente și Idempotency-Key
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from django.core.mail import EmailMultiAlternatives
|
|
29
|
+
|
|
30
|
+
email = EmailMultiAlternatives(
|
|
31
|
+
subject="Factura #1042",
|
|
32
|
+
body="Mulțumim pentru comandă!",
|
|
33
|
+
to=["client@exemplu.invalid"],
|
|
34
|
+
headers={"Idempotency-Key": "factura-1042"},
|
|
35
|
+
)
|
|
36
|
+
email.attach_alternative("<p>Mulțumim pentru comandă!</p>", "text/html")
|
|
37
|
+
email.attach("factura-1042.pdf", pdf_bytes, "application/pdf")
|
|
38
|
+
email.send()
|
|
39
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# django-serversmtp
|
|
2
|
+
|
|
3
|
+
EmailBackend oficial pentru **Django 4.2+** care trimite mesajele `django.core.mail` (`send_mail`, `EmailMessage`, `EmailMultiAlternatives`) prin API-ul HTTP **ServerSMTP** (`POST /v1/send`).
|
|
4
|
+
|
|
5
|
+
## Configurare (`settings.py`)
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
EMAIL_BACKEND = "django_serversmtp.ServerSMTPEmailBackend"
|
|
9
|
+
SERVERSMTP_API_KEY = os.environ["SERVERSMTP_API_KEY"]
|
|
10
|
+
DEFAULT_FROM_EMAIL = "Facturi <facturi@exemplu.invalid>"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Trimitere cu atașamente și Idempotency-Key
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from django.core.mail import EmailMultiAlternatives
|
|
17
|
+
|
|
18
|
+
email = EmailMultiAlternatives(
|
|
19
|
+
subject="Factura #1042",
|
|
20
|
+
body="Mulțumim pentru comandă!",
|
|
21
|
+
to=["client@exemplu.invalid"],
|
|
22
|
+
headers={"Idempotency-Key": "factura-1042"},
|
|
23
|
+
)
|
|
24
|
+
email.attach_alternative("<p>Mulțumim pentru comandă!</p>", "text/html")
|
|
25
|
+
email.attach("factura-1042.pdf", pdf_bytes, "application/pdf")
|
|
26
|
+
email.send()
|
|
27
|
+
```
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "django-serversmtp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Django EmailBackend for the ServerSMTP HTTP API (POST /v1/send)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"serversmtp>=0.3.0",
|
|
14
|
+
"Django>=4.2",
|
|
15
|
+
]
|
|
16
|
+
keywords = ["django", "email", "serversmtp", "transactional"]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Documentation = "https://serversmtp.ro/docs#biblioteci"
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.packages.find]
|
|
22
|
+
where = ["src"]
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Django BaseEmailBackend sending messages through ServerSMTP POST /v1/send."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
from email.mime.base import MIMEBase
|
|
7
|
+
from typing import Any, Iterable, Optional
|
|
8
|
+
|
|
9
|
+
from django.conf import settings
|
|
10
|
+
from django.core.mail.backends.base import BaseEmailBackend
|
|
11
|
+
from serversmtp import ServerSMTP
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ServerSMTPEmailBackend(BaseEmailBackend):
|
|
15
|
+
"""Sends Django EmailMessage / EmailMultiAlternatives via the ServerSMTP HTTP API."""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
api_key: Optional[str] = None,
|
|
20
|
+
base_url: Optional[str] = None,
|
|
21
|
+
fail_silently: bool = False,
|
|
22
|
+
**kwargs: Any,
|
|
23
|
+
) -> None:
|
|
24
|
+
super().__init__(fail_silently=fail_silently, **kwargs)
|
|
25
|
+
key = api_key or getattr(settings, "SERVERSMTP_API_KEY", None)
|
|
26
|
+
url = base_url or getattr(settings, "SERVERSMTP_BASE_URL", "https://api.serversmtp.ro")
|
|
27
|
+
self.client = ServerSMTP(key, base_url=url)
|
|
28
|
+
|
|
29
|
+
@staticmethod
|
|
30
|
+
def build_payload(message: Any) -> tuple[dict[str, Any], Optional[str]]:
|
|
31
|
+
payload: dict[str, Any] = {
|
|
32
|
+
"from": message.from_email,
|
|
33
|
+
"to": list(message.to or []),
|
|
34
|
+
"subject": str(message.subject or ""),
|
|
35
|
+
}
|
|
36
|
+
if getattr(message, "cc", None):
|
|
37
|
+
payload["cc"] = list(message.cc)
|
|
38
|
+
if getattr(message, "bcc", None):
|
|
39
|
+
payload["bcc"] = list(message.bcc)
|
|
40
|
+
if getattr(message, "reply_to", None):
|
|
41
|
+
payload["replyTo"] = message.reply_to[0]
|
|
42
|
+
|
|
43
|
+
html_body = None
|
|
44
|
+
if getattr(message, "content_subtype", "plain") == "html":
|
|
45
|
+
html_body = message.body
|
|
46
|
+
else:
|
|
47
|
+
payload["text"] = message.body or ""
|
|
48
|
+
for content, mimetype in getattr(message, "alternatives", []) or []:
|
|
49
|
+
if mimetype == "text/html":
|
|
50
|
+
html_body = content
|
|
51
|
+
break
|
|
52
|
+
if html_body:
|
|
53
|
+
payload["html"] = html_body
|
|
54
|
+
if not payload.get("html") and not payload.get("text"):
|
|
55
|
+
payload["text"] = " "
|
|
56
|
+
|
|
57
|
+
attachments = []
|
|
58
|
+
for attachment in getattr(message, "attachments", []) or []:
|
|
59
|
+
if isinstance(attachment, MIMEBase):
|
|
60
|
+
raw = attachment.get_payload(decode=True) or b""
|
|
61
|
+
filename = attachment.get_filename() or f"attachment-{len(attachments) + 1}"
|
|
62
|
+
mimetype = attachment.get_content_type() or "application/octet-stream"
|
|
63
|
+
elif isinstance(attachment, (tuple, list)) and len(attachment) >= 2:
|
|
64
|
+
filename, content = attachment[0], attachment[1]
|
|
65
|
+
mimetype = attachment[2] if len(attachment) > 2 and attachment[2] else "application/octet-stream"
|
|
66
|
+
raw = content.encode("utf-8") if isinstance(content, str) else bytes(content)
|
|
67
|
+
else:
|
|
68
|
+
continue
|
|
69
|
+
attachments.append({
|
|
70
|
+
"filename": str(filename),
|
|
71
|
+
"content": base64.b64encode(raw).decode("ascii"),
|
|
72
|
+
"contentType": str(mimetype),
|
|
73
|
+
})
|
|
74
|
+
if attachments:
|
|
75
|
+
payload["attachments"] = attachments
|
|
76
|
+
|
|
77
|
+
headers = getattr(message, "extra_headers", {}) or {}
|
|
78
|
+
idempotency_key = None
|
|
79
|
+
for key, value in headers.items():
|
|
80
|
+
if str(key).lower() == "idempotency-key" and value:
|
|
81
|
+
idempotency_key = str(value).strip()
|
|
82
|
+
break
|
|
83
|
+
|
|
84
|
+
return payload, idempotency_key
|
|
85
|
+
|
|
86
|
+
def send_messages(self, email_messages: Iterable[Any]) -> int:
|
|
87
|
+
sent = 0
|
|
88
|
+
for message in email_messages or []:
|
|
89
|
+
if not getattr(message, "to", None):
|
|
90
|
+
continue
|
|
91
|
+
try:
|
|
92
|
+
payload, key = self.build_payload(message)
|
|
93
|
+
self.client.emails.send(payload, idempotency_key=key)
|
|
94
|
+
sent += 1
|
|
95
|
+
except Exception:
|
|
96
|
+
if not self.fail_silently:
|
|
97
|
+
raise
|
|
98
|
+
return sent
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-serversmtp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Django EmailBackend for the ServerSMTP HTTP API (POST /v1/send)
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Documentation, https://serversmtp.ro/docs#biblioteci
|
|
7
|
+
Keywords: django,email,serversmtp,transactional
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: serversmtp>=0.3.0
|
|
11
|
+
Requires-Dist: Django>=4.2
|
|
12
|
+
|
|
13
|
+
# django-serversmtp
|
|
14
|
+
|
|
15
|
+
EmailBackend oficial pentru **Django 4.2+** care trimite mesajele `django.core.mail` (`send_mail`, `EmailMessage`, `EmailMultiAlternatives`) prin API-ul HTTP **ServerSMTP** (`POST /v1/send`).
|
|
16
|
+
|
|
17
|
+
## Configurare (`settings.py`)
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
EMAIL_BACKEND = "django_serversmtp.ServerSMTPEmailBackend"
|
|
21
|
+
SERVERSMTP_API_KEY = os.environ["SERVERSMTP_API_KEY"]
|
|
22
|
+
DEFAULT_FROM_EMAIL = "Facturi <facturi@exemplu.invalid>"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Trimitere cu atașamente și Idempotency-Key
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from django.core.mail import EmailMultiAlternatives
|
|
29
|
+
|
|
30
|
+
email = EmailMultiAlternatives(
|
|
31
|
+
subject="Factura #1042",
|
|
32
|
+
body="Mulțumim pentru comandă!",
|
|
33
|
+
to=["client@exemplu.invalid"],
|
|
34
|
+
headers={"Idempotency-Key": "factura-1042"},
|
|
35
|
+
)
|
|
36
|
+
email.attach_alternative("<p>Mulțumim pentru comandă!</p>", "text/html")
|
|
37
|
+
email.attach("factura-1042.pdf", pdf_bytes, "application/pdf")
|
|
38
|
+
email.send()
|
|
39
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/django_serversmtp/__init__.py
|
|
4
|
+
src/django_serversmtp/backend.py
|
|
5
|
+
src/django_serversmtp.egg-info/PKG-INFO
|
|
6
|
+
src/django_serversmtp.egg-info/SOURCES.txt
|
|
7
|
+
src/django_serversmtp.egg-info/dependency_links.txt
|
|
8
|
+
src/django_serversmtp.egg-info/requires.txt
|
|
9
|
+
src/django_serversmtp.egg-info/top_level.txt
|
|
10
|
+
tests/test_backend.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
django_serversmtp
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import sys
|
|
3
|
+
import types
|
|
4
|
+
import unittest
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
ROOT = Path(__file__).resolve().parents[1] / "src"
|
|
8
|
+
sys.path.insert(0, str(ROOT))
|
|
9
|
+
|
|
10
|
+
# Minimal stubs so the payload builder test runs without Django installed globally.
|
|
11
|
+
django_mod = types.ModuleType("django")
|
|
12
|
+
django_conf = types.ModuleType("django.conf")
|
|
13
|
+
django_conf.settings = types.SimpleNamespace(SERVERSMTP_API_KEY="em_live_" + "a" * 48)
|
|
14
|
+
django_mail_base = types.ModuleType("django.core.mail.backends.base")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class BaseEmailBackend:
|
|
18
|
+
def __init__(self, fail_silently=False, **kwargs):
|
|
19
|
+
self.fail_silently = fail_silently
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
django_mail_base.BaseEmailBackend = BaseEmailBackend
|
|
23
|
+
sys.modules["django"] = django_mod
|
|
24
|
+
sys.modules["django.conf"] = django_conf
|
|
25
|
+
sys.modules["django.core"] = types.ModuleType("django.core")
|
|
26
|
+
sys.modules["django.core.mail"] = types.ModuleType("django.core.mail")
|
|
27
|
+
sys.modules["django.core.mail.backends"] = types.ModuleType("django.core.mail.backends")
|
|
28
|
+
sys.modules["django.core.mail.backends.base"] = django_mail_base
|
|
29
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "python" / "src"))
|
|
30
|
+
|
|
31
|
+
from django_serversmtp.backend import ServerSMTPEmailBackend # noqa: E402
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class BackendPayloadTest(unittest.TestCase):
|
|
35
|
+
def test_builds_multialternative_payload_with_attachments_and_idempotency(self):
|
|
36
|
+
msg = types.SimpleNamespace(
|
|
37
|
+
from_email="Facturi <facturi@exemplu.invalid>",
|
|
38
|
+
to=["client@exemplu.invalid"],
|
|
39
|
+
cc=["contabil@exemplu.invalid"],
|
|
40
|
+
bcc=["arhiva@exemplu.invalid"],
|
|
41
|
+
reply_to=["suport@exemplu.invalid"],
|
|
42
|
+
subject="Factura #1042",
|
|
43
|
+
body="Text simplu",
|
|
44
|
+
content_subtype="plain",
|
|
45
|
+
alternatives=[("<p>HTML</p>", "text/html")],
|
|
46
|
+
attachments=[("factura.pdf", b"%PDF-1.4", "application/pdf")],
|
|
47
|
+
extra_headers={"Idempotency-Key": "factura-1042"},
|
|
48
|
+
)
|
|
49
|
+
payload, key = ServerSMTPEmailBackend.build_payload(msg)
|
|
50
|
+
self.assertEqual(key, "factura-1042")
|
|
51
|
+
self.assertEqual(payload["html"], "<p>HTML</p>")
|
|
52
|
+
self.assertEqual(payload["text"], "Text simplu")
|
|
53
|
+
self.assertEqual(payload["cc"], ["contabil@exemplu.invalid"])
|
|
54
|
+
self.assertEqual(payload["bcc"], ["arhiva@exemplu.invalid"])
|
|
55
|
+
self.assertEqual(payload["replyTo"], "suport@exemplu.invalid")
|
|
56
|
+
self.assertEqual(base64.b64decode(payload["attachments"][0]["content"]), b"%PDF-1.4")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
if __name__ == "__main__":
|
|
60
|
+
unittest.main()
|