Get to know ben_p with this User Highlight Learn More

How to setup mail recipients using variables with list of email addresses in a Dataset

Level 2
How to setup mail recipients using variables with list of email addresses in a Dataset

Hi All,

I would like to seek guidance to setup a scenario that sends email to a lists of email addresses from a particular field in Dataset  as recipients (within the same projects). 

There will be different list of email address each time a job is completed. 

Some examples are greatly appreciated. Thanks.

 

4 Replies
Community Manager
Community Manager

Hi @thianjin we recent published a Knowledge Base article, "How to programmatically set email recipients in a "Send email" reporter using the API?" that we believe, at least in part, helps answer your question(s). 

I hope this helps and please let us know if have any further questions.

Don't forget to mark as "Accepted Solution" when someone provides the correct answer to your question.
0 Kudos
Level 2
Author

😉 It works for the most part. I had some errors when I try to alter the codes with data from DSS dataset.

# Example: load a DSS dataset as a Pandas dataframe
mydataset = dataiku.Dataset("orders")
myemail_df = mydataset.get_dataframe(columns=['customer_email'])

scenarios_list = project.list_scenarios()
#new_recipient_email_address = "john.doe@here.com"
new_recipient_email_address = myemail_df 

0 Kudos
Dataiker
Dataiker

Hi @thianjin ,

It seems that you're sending a Dataframe to a variable which expects a string.

If the list of recipients read from your dataset is exhaustive, and you don't need to preserve the list of the already existing recipients you can just convert it into a comma-separated string and replace the value of the recipient attribute of the scenario reporter that you want to update.

The code sample below will update the recipients of all the send email reporters for a specific scenario.

import dataiku

client = dataiku.api_client()

project = client.get_project("PROJECT_KEY")

mydataset = dataiku.Dataset("orders")
myemails_df = mydataset.get_dataframe(columns=['customer_email'])
myemails = ', '.join(myemails_df['customer_email'].tolist()) # Convert the Dataframe into a string of comma-separated values

scenario = project.get_scenario("SCENARIO_ID")
scenario_definition = scenario.get_definition(with_status=False)

for i in range(0, len(scenario_definition['reporters'])):
if scenario_definition['reporters'][i]['messaging']['type'] == "mail-scenario":
scenario_definition['reporters'][i]['messaging']['configuration']['recipient'] = myemails

scenario.set_definition(scenario_definition,with_status=False)

 Have a great day!

Level 2
Author

Thank you for helping.

Labels (2)