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

Emails : finaliser la configuration d'envoi d'-mails transactionnels avec Brevo #1033

Merged
merged 2 commits into from
Jan 16, 2024
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
4 changes: 2 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@

EMAIL_BACKEND = "anymail.backends.mailjet.EmailBackend"

DEFAULT_FROM_EMAIL = "noreply@inclusion.beta.gouv.fr"
DEFAULT_FROM_NAME = "Marché de l'inclusion"
DEFAULT_FROM_EMAIL = "ne-pas-repondre@lemarche.inclusion.beta.gouv.fr"
DEFAULT_FROM_NAME = "Le Marché de l'inclusion"
CONTACT_EMAIL = env("CONTACT_EMAIL", default="[email protected]")
TEAM_CONTACT_EMAIL = env("TEAM_CONTACT_EMAIL", default="[email protected]")
NOTIFY_EMAIL = env("NOTIFY_EMAIL", default="[email protected]")
Expand Down
69 changes: 31 additions & 38 deletions lemarche/utils/apis/api_brevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,6 @@ def get_api_client():
return sib_api_v3_sdk.ApiClient(config)


@task()
def send_html_email(to: list, sender: dict, html_content: str, headers: dict = {}):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client)
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
to=to, headers=headers, html_content=html_content, sender=sender
) # SendSmtpEmail | Values to send a transactional email
try:
# Send a transactional email
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)


@task()
def send_transactionnel_email(to: list, sender: dict, template_id: int, params_template: dict, headers: dict = {}):
"""Send transactionnel email

Args:
to (list): List of dict, ex : [{"email": "[email protected]", "name": "John Doe"}]
template_id (int): template id of email
params_template (dict): Paramaters of template, ec {"name": "John", "surname": "Doe"}
headers (dict, optional): Custom headers of emails. Defaults to {}.
"""
api_client = get_api_client()
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client)
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
to=to, template_id=template_id, sender=sender, params=params_template, headers=headers
) # SendSmtpEmail | Values to send a transactional email
try:
# Send a transactional email
api_response = api_instance.send_transac_email(send_smtp_email)
print(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)


def create_contact(user: User, list_id: int):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.ContactsApi(api_client)
Expand Down Expand Up @@ -101,3 +63,34 @@ def remove_contact_from_list(user: User, list_id: int):
logger.info("calling Brevo->ContactsApi->remove_contact_from_list: contact doesn't exist in this list")
else:
logger.error("Exception when calling Brevo->ContactsApi->remove_contact_from_list: %s\n" % e)


@task()
def send_transactional_email_with_template(
template_id: int,
subject: str,
recipient_email: str,
recipient_name: str,
variables: dict,
from_email=settings.DEFAULT_FROM_EMAIL,
from_name=settings.DEFAULT_FROM_NAME,
):
api_client = get_api_client()
api_instance = sib_api_v3_sdk.TransactionalEmailsApi(api_client)
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(
sender={"email": from_email, "name": from_name},
to=[{"email": recipient_email, "name": recipient_name}],
subject=subject,
template_id=template_id,
params=variables,
)

if settings.BITOUBI_ENV not in ENV_NOT_ALLOWED:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est fait comme ça partout, mais je pense qu'on peut avoir une réflexion sur ce ENV_NOT_ALLOWED, pour peut-être le centraliser dans le settings et l'inverser pour éviter les erreurs (ENV_ALLOWED).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

en effet !

try:
response = api_instance.send_transac_email(send_smtp_email)
logger.info("Brevo: send transactional email with template")
return response
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
else:
logger.info("Brevo: email not sent (DEV or TEST environment detected)")