Sending HTML Email from Databricks Using Python Produces Attribute Error: 'list' object has no attribute ' encode'
Sending HTML Email from Databricks Using Python Produces Attribute Error: 'list' object has no attribute ' encode'
我正在尝试使用 Python 从 Databricks 发送电子邮件。我想使用 HTML 格式化我的电子邮件内容,但是我收到错误 AttributeError:'list' object has no attribute 'encode'
尽管查看了无数博客、知识库文章和其他堆栈溢出条目,但我仍无法弄清楚如何解决此问题。我没有编程背景,所以我有可能(很可能?)忽略了一些非常简单的事情,但我将非常感谢对此的帮助。
我的笔记本有以下3条命令
命令1
%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)
命令2
%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>
"""
]
命令3
%py
sendEmail(from_addr, to_addr, "My Email Subject Line", text)
Cmd1 和 Cmd2 运行 没问题,但我在 Cmd3 上遇到错误。谁能帮帮我?
完整的错误消息如下所示:
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'
看起来像
msg.attach(MIMEText(messageHTML, 'html'))
期望消息 HTML 的类型为 'str' 而不是 'list'。尝试更改:
messageHTML = [
"<center><h1>Example Email Title</h1></center>",
"""
<p><b>A standard paragraph.</b></p>
<ol>
<li>This demonstrates a list.</li>
</ol>
"""
]
到
messageHTML = """
<center><h1>Example Email Title</h1></center>
<p><b>A standard paragraph.</b></p>
<ol>
<li>This demonstrates a list.</li>
</ol>
"""
看看是否可行。
我正在尝试使用 Python 从 Databricks 发送电子邮件。我想使用 HTML 格式化我的电子邮件内容,但是我收到错误 AttributeError:'list' object has no attribute 'encode'
尽管查看了无数博客、知识库文章和其他堆栈溢出条目,但我仍无法弄清楚如何解决此问题。我没有编程背景,所以我有可能(很可能?)忽略了一些非常简单的事情,但我将非常感谢对此的帮助。
我的笔记本有以下3条命令
命令1
%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)
命令2
%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>
"""
]
命令3
%py
sendEmail(from_addr, to_addr, "My Email Subject Line", text)
Cmd1 和 Cmd2 运行 没问题,但我在 Cmd3 上遇到错误。谁能帮帮我?
完整的错误消息如下所示:
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'
看起来像
msg.attach(MIMEText(messageHTML, 'html'))
期望消息 HTML 的类型为 'str' 而不是 'list'。尝试更改:
messageHTML = [
"<center><h1>Example Email Title</h1></center>",
"""
<p><b>A standard paragraph.</b></p>
<ol>
<li>This demonstrates a list.</li>
</ol>
"""
]
到
messageHTML = """
<center><h1>Example Email Title</h1></center>
<p><b>A standard paragraph.</b></p>
<ol>
<li>This demonstrates a list.</li>
</ol>
"""
看看是否可行。