cancel
Showing results for 
Search instead for 
Did you mean: 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 

Send formatted html email from email distribution address

mchirouze
New Contributor

Hi, I have created an email distribution list "#MyList@mycompany.com". In the RShiny world I was able to send emails by a) getting the IP of the server I was sending the emails from and b) whitelisting that IP address within my company's SMTP Relay registry. At that point I could send my email using a function like this:

      mailR::send.mail(from = '#MyList@mycompany.com', to = sendto,
                       subject = subject, body = email_fname,
                       attach.files = attach.files, html = T, inline = T,
                       smtp = list(host.name=smtpRelay, port = smtPort),
                       authenticate = F, send = T)

What do I need to do to set up a similar service in databricks?

1 REPLY 1

Kaniz_Fatma
Community Manager
Community Manager

Hi @mchirouzeTo set up email services in Databricks, you have a few options depending on your requirements. Let’s explore them:

  1. Workspace Email Settings:

  2. Job Notifications:

    • If you want to receive email notifications specifically for job events (such as job runs beginning, completing, or failing), follow these steps:
  3. Sending Custom Emails from a Databricks Notebook:

    • If you need to send custom emails from a Databricks notebook, you can use Python code.
    • Here’s an example of how to send an email with an attachment from a Databricks notebook using Python:
      import smtplib
      from pathlib import Path
      from email.mime.multipart import MIMEMultipart
      from email.mime.base import MIMEBase
      from email.mime.text import MIMEText
      from email.utils import COMMASPACE, formatdate
      from email import encoders
      
      def send_mail(send_from, send_to, subject, message, files, server, port, username, password, use_tls=True):
          msg = MIMEMultipart()
          msg['From'] = send_from
          msg['To'] = COMMASPACE.join(send_to)
          msg['Date'] = formatdate(localtime=True)
          msg['Subject'] = subject
          msg.attach(MIMEText(message))
      
          for path in files:
              part = MIMEBase('application', "octet-stream")
              with open(path, 'rb') as file:
                  part.set_payload(file.read())
              encoders.encode_base64(part)
              part.add_header('Content-Disposition', f'attachment; filename="{Path(path).name}"')
              msg.attach(part)
      
          smtp = smtplib.SMTP(server, port)
          if use_tls:
              smtp.starttls()
          smtp.login(username, password)
          smtp.sendmail(send_from, send_to, msg.as_string())
          smtp.quit()
      
      # Example usage:
      send_mail(send_from="your_email@example.com",
                send_to=["recipient1@example.com", "recipient2@example.com"],
                subject="Test Email",
                message="This is a test email from Databricks.",
                files=["/dbfs/mnt/<Mount Point Directory>/file.txt"],
                server="<SMTP Host>",
                port=<SMTP Port>,
                username="<SMTP Username>",
                password="<SMTP Password>")
      
    • Make sure to adjust the placeholders (<...>) with your actual values, including the file path on DBF...3.
  4. Amazon SES Integration:

Choose the option that best fits your use case, and you’ll be able to set up email services within Databricks. 📧

 
Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!