Hi there. Is it possible to use Prefect cloud to s...
# prefect-community
p
Hi there. Is it possible to use Prefect cloud to send a notification from a state handler? I need to send an email conditionally depending on the result of a task.
c
Hi Pedro - sure! You don’t need Prefect Cloud to achieve this though; you can run any code within a state handler that you want based on the state of the task its associated it. To get some inspiration for notifications, you can check some of our pre-built state handlers here: https://docs.prefect.io/api/latest/utilities/notifications.html#functions
p
Hi Chris, I guess my question is whether there is a way to send an email notification without having to provide my own smtp server.
c
I see, you can do this at the flow level but it’s only going to be conditional on the Prefect State, not the result of any specific task. Cloud doesn’t have access to your underlying data, so you can’t currently condition on data on the Cloud-side (but you can within your own code)
p
Got it. I didn't see a generic smtp notification only
gmail_notifier
would I have to code from scratch or is there another place where I should be looking?
c
I suggest repurposing the code from the `EmailTask`: https://github.com/PrefectHQ/prefect/blob/master/src/prefect/tasks/notifications/email_task.py Note that you can either: - use this task directly as a downstream dependency of your task - reimplement this task’s logic as a state handler Which you choose is largely personal preference
p
If I use this as a task, I'd have to add use
case
to make the notification conditional instead of relying on the state handler. Correct?
c
You could use
case
for sure, but if it were me I’d probably do the following:
Copy code
class CustomEmailTask(EmailTask):
    def run(self, data, **kwargs):
        # perform conditional check on data
        if conditional_check:
            return super().run(**kwargs)
        else:
            <http://self.logger.info|self.logger.info>("Conditional check did not pass, not sending email.")
            raise prefect.engine.signals.SKIP("No need to send email.")
I prefer fewer tasks and more use of Prefect States / signals, but that’s just a personal preference
p
Cool. Thanks for your help!
c
anytime!