Way to send SQL query results from a Databricks notebook via email

faizan_111111
Databricks Partner

Hi everyone,

I’m working with a Databricks notebook where I run a SQL query using spark.sql. The query returns a small result set (mainly counts or summary values).

Once the notebook finishes running, I need to automatically send the query result from the Databricks notebook to an email (Outlook).

I’m looking for the simplest and commonly used approach to do this directly from a Databricks notebook, just a clean and reliable solution.

Any guidance or best practices would be appreciated.

Thanks

Marc_Gibson96
Contributor

Hi @faizan_111111,

I would suggest setting up an email destination around a SQL Alert which should give you the ability to see returned results in the QUERY_RESULT_TABLEvariable. See: https://docs.databricks.com/aws/en/sql/user/alerts/

If this doesn't fit your use case exactly you could look at various python libraries that are designed to send emails. e.g https://docs.python.org/3/library/smtplib.html

View solution in original post