Search code examples
pythonhtmldatabrickssmtplib

Sending HTML Email from Databricks Using Python Produces Attribute Error: 'list' object has no attribute ' encode'


I'm trying to send an email from Databricks using Python. I want to format my email content using HTML, however and I'm getting the error AttributeError:'list' object has no attribute 'encode'

I can't figure out how to fix this despite looking at countless blogs, knowledge base articles, and other stack overflow entries. I don't have a programming background, so it's possible (probable?) that I'm overlooking something very simple, but I would greatly appreciate help with this.

My notebook has the following 3 commands

Cmd1

%py

import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.application import MIMEApplication

def sendEmail(sender, receivers, subject, text):
  msg = MIMEMultipart()  
  msg['From'] = sender
  msg['BCC'] = ', '.join(receivers)
  msg['Subject'] = subject
  msg.attach(MIMEText(messageHTML, 'html'))
  text = msg.as_string()
  smtpObj = smtplib.SMTP('mySMTP')
  
  smtpObj.sendmail(sender, receivers, text)

Cmd2

%py
from_addr = 'addr1@mail.com'
to_addr = 'addr2@mail.com'

messageHTML = [
  "<center><h1>Example Email Title</h1></center>",
  """
  <p><b>A standard paragraph.</b></p>
  <ol>
      <li>This demonstrates a list.</li>
  </ol>
  """
]

Cmd3

%py
sendEmail(from_addr, to_addr, "My Email Subject Line", text)

Cmd1 and Cmd2 run with no issue, but I get the error on Cmd3. Can anyone please help me?

Here is what the full error message looks like:

AttributeError: 'list' object has no attribute 'encode'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<command-6158222> in <module>
----> 1 sendEmail(from_addr, to_addr, "My Email Subject Line", text)

<command-6158220> in sendEmail(sender, receivers, subject, text)
     12   msg['BCC'] = ', '.join(receivers)
     13   msg['Subject'] = subject
---> 14   msg.attach(MIMEText(messageHTML, 'html'))
     15   text = msg.as_string()
     16   smtpObj = smtplib.SMTP('mySMTP')

/databricks/python/lib/python3.7/email/mime/text.py in __init__(self, _text, _subtype, _charset, policy)
     32         if _charset is None:
     33             try:
---> 34                 _text.encode('us-ascii')
     35                 _charset = 'us-ascii'
     36             except UnicodeEncodeError:

AttributeError: 'list' object has no attribute 'encode'

Solution

  • It looks like

    msg.attach(MIMEText(messageHTML, 'html'))
    

    is expecting a messageHTML to be of type 'str' not 'list'. Try changing:

    messageHTML = [
      "<center><h1>Example Email Title</h1></center>",
      """
      <p><b>A standard paragraph.</b></p>
      <ol>
          <li>This demonstrates a list.</li>
      </ol>
      """
    ]
    

    to

    messageHTML = """
      <center><h1>Example Email Title</h1></center>
      <p><b>A standard paragraph.</b></p>
      <ol>
          <li>This demonstrates a list.</li>
      </ol>
      """
    

    and see if that works.