cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Databricks Free Edition Help
Engage in discussions about the Databricks Free Edition within the Databricks Community. Share insights, tips, and best practices for getting started, troubleshooting issues, and maximizing the value of your trial experience to explore Databricks' capabilities effectively.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Databricks Workflow for Sharing Delta Table Data via Email (Text & Attachment)

SantiNath_Dey
Contributor
Hi All,
We have data in a Delta table that needs to be shared via email as both inline text and an attachment using a Databricks workflow. Could you please assist with this? If possible, kindly share a sample code as well.
4 REPLIES 4

anmolhhns
New Contributor

Hi @SantiNath_Dey Databricks jobs/workflows handle orchestration be only for status alerts(Success, Failure, Completion), Delta table data can't be send as content of the email, To send delta table as an email you will need a workflow task that reads the table and build an email, saves the same data as a CSV/Excel file (for the attachment), and send it through external email service like SMTP, Microsoft graph etc. 

Ashwin_DSA
Databricks Employee
Databricks Employee

Hi @SantiNath_Dey,

There isnโ€™t a native feature to embed a Delta table directly into an email body.

You can build custom code to read the Delta table, format part of it as HTML, and send it through your companyโ€™s email service if one is available. In practice, Iโ€™d recommend showing only a small preview in the email rather than trying to inline the full table, and then attaching a file only if that is truly required.

That said, this approach can get complicated fairly quickly. It adds custom logic, ongoing maintenance overhead, and potentially compliance concerns if the table contains PII or other sensitive data.

If there is a hard requirement to send a file, then yes, generating an attachment is possible. But as a best practice, I would usually recommend...

  • AI/BI Dashboards for internal stakeholders, so they always have access to the latest data
  • Delta Sharing for external consumers or downstream tools that need direct access to the data
  • Email attachments only when someone explicitly needs an offline copy

The main downside with attachments is that they go stale immediately, and large files can also hit email size limits or get blocked by mail systems. A live dashboard or shared table is usually a cleaner and more maintainable approach over time.

If this answer resolves your question, could you mark it as โ€œAccept as Solutionโ€? That helps other users quickly find the correct fix.

Regards,
Ashwin | Delivery Solution Architect @ Databricks
Helping you build and scale the Data Intelligence Platform.
***Opinions are my own***

balajij8
Contributor III

@SantiNath_Dey 

You can follow below

  • Python Task - You can use a Python Task within a Databricks Workflow. It's ideal if you require strict HTML format with high flexibility. Python script can query the Delta table and the full dataset can be written to a Unity Catalog Volume as CSV or Excel file. You can use smtplib or others to send the email with the summary injected in the body of the email and the file is attached directly from the Volume.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
 
fromaddr = "EMAIL address of the sender"
toaddr = "EMAIL address of the receiver"
 
# instance of MIMEMultipart
msg = MIMEMultipart()

# storing the senders email address  
msg['From'] = fromaddr

# storing the receivers email address 
msg['To'] = toaddr

# storing the subject 
msg['Subject'] = "Subject of the Mail"

# string to store the body of the mail
body = "Body_of_the_mail"

# attach the body with the msg instance
msg.attach(MIMEText(body, 'plain'))

# open the file to be sent 
filename = "File_name_with_extension"
attachment = open("Path of the file", "rb")

# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form
p.set_payload((attachment).read())

# encode into base64
encoders.encode_base64(p)
 
p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# attach the instance 'p' to instance 'msg'
msg.attach(p)

# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)

# start TLS for security
s.starttls()

# Authentication
s.login(fromaddr, "Password_of_the_sender")

# Converts the Multipart msg into a string
text = msg.as_string()

# sending the mail
s.sendmail(fromaddr, toaddr, text)

# terminating the session
s.quit()
  • Databricks Dashboard Subscriptions - Its preferred method using native Dashboards that leverages built in scheduling to distribute reports without writing any code. You can add specific users or Notification Destinations (email groups) to be notified automatically after workflow is run. Subscribers get a PDF snapshot and can optionally include tabular data from selected dashboard widgets as CSV, TSV, or Excel attachments.

SantiNath_Dey
Contributor

thank you for quick response