Django app to store emails in db
pip install jemail
pip install django-anymail[sendgrid]
Anymail used as email backend, for now jemail tested only with sendgrid
backend.
Update settings with:
EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"
# defaults
JEMAIL = {
"METADATA_ID_KEY": "message_id", # tag name for EmailMessage.pk in message metadata
"HTML_MESSAGE_UPLOAD_TO": "emails/messages/", # path to save html messages to
"ATTACHMENT_UPLOAD_TO": "emails/attachments/", # path to save attachments to
# "IMPORT_HTML_MESSAGE_UPLOAD_TO": "myproject.utils.message_upload_to", # callable option
# "IMPORT_ATTACHMENT_UPLOAD_TO": "myproject.utils.attachment_upload_to", # callable option
}
from jemail import EmailMessage, EmailAttachment
# save email in db
EmailMessage.objects.create_with_objects(
to=["[email protected]"],
subject="Subject",
# if body not provided it derived from html_message
# using html2text library
body="Hi User,...",
html_message="<p>Hi User...",
# the rest is optional
from_email="[email protected]",
cc=["[email protected]"],
bcc=["[email protected]"],
attachments=[
EmailAttachment.objects.create(
filename="doc.pdf",
mimetype="application/pdf",
file=ContentFile(b"...", name="doc.pdf"),
)
],
reply_to=["Example Team <[email protected]>"],
created_by_id=user.pk,
)
# build EmailMultiAlternatives from db
msg = EmailMessage.objects.get(pk=pk).build_message()
# send email
msg.send()
nix-direnv:
echo "use flake" >> .envrc
direnv allow
app.install
pytest
nix:
nix develop
app.install
pytest
uv:
uv -q venv .venv
source .venv/bin/activate
uv pip install -e .[dev,test]
pre-commit install
pytest