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

Sendinblue : méthodes pour envoyer des e-mails transactionnels #1985

Merged
merged 3 commits into from
Jun 25, 2023
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
8 changes: 5 additions & 3 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,18 @@
EMAIL_BACKEND = "anymail.backends.sendinblue.EmailBackend"

CONTACT_EMAIL = os.getenv("CONTACT_EMAIL")
DEFAULT_FROM_EMAIL = "Quiz de l'Anthropocène <[email protected]>"
DEFAULT_FROM_EMAIL = "[email protected]"
DEFAULT_FROM_NAME = "Quiz de l'Anthropocène"
SERVER_EMAIL = os.getenv("TECH_EMAIL")
ADMINS = eval(os.getenv("ADMINS", "[]"))

SIB_CONTACT_ENDPOINT = "https://api.sendinblue.com/v3/contacts"
SIB_CONTACT_DOI_ENDPOINT = "https://api.sendinblue.com/v3/contacts/doubleOptinConfirmation"
SIB_CONTRIBUTOR_LIST_ID = os.getenv("SIB_CONTRIBUTOR_LIST_ID", 0)
SIB_NEWSLETTER_LIST_ID = os.getenv("SIB_NEWSLETTER_LIST_ID", 0)
SIB_NEWSLETTER_DOI_TEMPLATE_ID = os.getenv("SIB_NEWSLETTER_DOI_TEMPLATE_ID", 0)
SIB_CONTACT_ENDPOINT = "https://api.sendinblue.com/v3/contacts"
SIB_CONTACT_DOI_ENDPOINT = "https://api.sendinblue.com/v3/contacts/doubleOptinConfirmation"

SIB_SMTP_ENDPOINT = "https://api.brevo.com/v3/smtp/email"


# Errors
Expand Down
50 changes: 50 additions & 0 deletions core/utils/sendinblue.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def add_to_contact_list(user, list_id=settings.SIB_CONTRIBUTOR_LIST_ID, extra_at
if not settings.DEBUG and not settings.TESTING:
return requests.post(settings.SIB_CONTACT_ENDPOINT, headers=HEADERS, data=json.dumps(data))
else:
print("Sendinblue: user not added to contact list (DEBUG or TESTING environment detected)")
return True


Expand All @@ -57,4 +58,53 @@ def newsletter_registration(user_email):
if not settings.DEBUG and not settings.TESTING:
return requests.post(settings.SIB_CONTACT_DOI_ENDPOINT, headers=HEADERS, data=json.dumps(data))
else:
print("Sendinblue: user not registered to the newsletter (DEBUG or TESTING environment detected)")
return True


def send_transactional_email_with_template_id(
to_email,
to_name,
template_id,
parameters=None,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
):
data = {
"sender": {"email": from_email, "name": from_name}, # email must be a sender registered and verified in Brevo
"to": [{"email": to_email, "name": to_name}],
"templateId": template_id,
}
if parameters:
data["params"] = parameters

if not settings.DEBUG and not settings.TESTING:
return requests.post(settings.SIB_SMTP_ENDPOINT, headers=HEADERS, data=json.dumps(data))
else:
print("Sendinblue: email not sent (DEBUG or TESTING environment detected)")
return True


def send_transactional_email_with_html(
to_email,
to_name,
subject,
html,
parameters=None,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
):
data = {
"sender": {"email": from_email, "name": from_name}, # email must be a sender registered and verified in Brevo
"to": [{"email": to_email, "name": to_name}],
"subject": subject,
"htmlContent": html,
}
if parameters:
data["params"] = parameters

if not settings.DEBUG and not settings.TESTING:
return requests.post(settings.SIB_SMTP_ENDPOINT, headers=HEADERS, data=json.dumps(data))
else:
print("Sendinblue: email not sent (DEBUG or TESTING environment detected)")
return True