Email Notifications

The hello service generated by mvi init will send a notification if someone tries to greet “World” and you would be able to view the notification on the manager dashboard. In this tutorial we will see how the same notification can be send as an email. As a reminder, here is service.py for the hello service:

import logging
from mvi import service
from mvi.communication import notify, Severity

logger = logging.getLogger(__name__)

service.add_parameter("greeting_phrase", "Hello")

@service.entrypoint
def hello(name: str) -> str:
    greeting_phrase = service.get_parameter("greeting_phrase")
    if name == "World":
        notify(
            msg="Someone is trying to greet the World, too time consuming. Skipping!",
            severity=Severity.WARNING,
        )
        return "Greeting failed"
    logger.info(f"Greeting someone with the name: {name}")
    return f"{greeting_phrase} {name}"

if __name__ == '__main__':
    service.run()

Manager Configuration

For the manager to be able to send emails you must configure it with an email and an SMTP server. Please refer to this page on the manager configuration: Email notifications

SDK Syntax

By default, notify() with not send any emails. For a notification to become an email notification, you have to pass a list of email addresses to the emails keyword argument of notify()

import logging
from mvi import service
from mvi.communication import notify, Severity

logger = logging.getLogger(__name__)

service.add_parameter("greeting_phrase", "Hello")

@service.entrypoint
def hello(name: str) -> str:
    greeting_phrase = service.get_parameter("greeting_phrase")
    if name == "World":
        notify(
            msg="Someone is trying to greet the World, too time consuming. Skipping!",
            severity=Severity.WARNING,
            emails=["your@email.com"],
        )
        return "Greeting failed"
    logger.info(f"Greeting someone with the name: {name}")
    return f"{greeting_phrase} {name}"

if __name__ == '__main__':
    service.run()

To make the email recipients dynamically changeable, we can use this in conjunction with add_parameter() like so

import logging
from mvi import service
from mvi.communication import notify, Severity

logger = logging.getLogger(__name__)

service.add_parameter("greeting_phrase", "Hello")
service.add_parameter("emails", ["your@email.com"])

@service.entrypoint
def hello(name: str) -> str:
    greeting_phrase = service.get_parameter("greeting_phrase")
    emails = service.get_parameter("emails")
    if name == "World":
        notify(
            msg="Someone is trying to greet the World, too time consuming. Skipping!",
            severity=Severity.WARNING,
            emails=emails,
        )
        return "Greeting failed"
    logger.info(f"Greeting someone with the name: {name}")
    return f"{greeting_phrase} {name}"

if __name__ == '__main__':
    service.run()

This way, email addresses can be added and removed. You could of course use different emails for different notifications, so different stakeholders get notified of different things.