Skip to content

Send mails

PERSEUS handles emails and notifications with the NotificationManager service. The emails to be sent are saved, checked with a running cronjob and sent (learn more about PERSEUS cronjobs here).

The function perseus.utils.add_notification() can be used to send and schedule emails. A timezone-aware datetime object can be passed with send_at, where you can specify when the mail should be sent.

Email templates can be used within states. The parent class BaseState offers two methods to send internal/external emails:

  • BaseState.send_internal_email_template(...)
  • BaseState.send_external_email_template(...)

The example below will send an email to all members of the ALLOCATION_BOARD_MEMBER role group with the internal template project_granted:

from perseus.states import BaseState
from perseus.datamanager import Project
from perseus.security import CoreRoleName
class ExportControl(BaseState):
...
@classmethod
def handle_action(cls, project: Project, task_id: str, content: dict):
...
ExportControl.send_internal_email_template(
project=project,
role=CoreRoleName.ALLOCATION_BOARD_MEMBER,
template_id="project_granted",
)

This example will send an email to the principal investigator (PI) and the person of contact (PC) of a project using the email template project_granted_export_control with a custom subject:

from perseus.states import BaseState
from perseus.datamanager import Project
class ExportControl(BaseState):
...
@classmethod
def handle_action(cls, project: Project, task_id: str, content: dict):
...
ExportControl.send_external_email_template(
project=project,
include_principal_investigator=True,
include_person_of_contact=True,
subject=f"Export control for project {project.abbreviation} successfully completed"
template_id="project_granted_export_control",
)

The following examples sends an email to all members of the ALLOCATION_BOARD_MEMBER role group. The email is sent immediately because no timezone-aware datetime object was passed with send_at.

from perseus.utils import add_notification
from perseus.security import CoreRoleName
from perseus.config import FRONTEND_URL
new_project = Project(...)
add_notification(
CoreRoleName.ALLOCATION_BOARD_MEMBER,
f"Added new proposal ({new_project.source.name}, #{new_project.source.foreign_id})",
f"Added new proposal ({new_project.source.name}, "
+ f"#{new_project.source.foreign_id})"
+ f"<br /><br /><a href='{FRONTEND_URL}/InitialState#{str(new_project.db_id)}'>View this task in PERSEUS</a>",
)