从 Databricks 笔记本发送电子邮件

Posted

技术标签:

【中文标题】从 Databricks 笔记本发送电子邮件【英文标题】:Sending Email From Databricks Notebooks 【发布时间】:2019-05-22 21:50:09 【问题描述】:

我想从 databricks 笔记本发送电子邮件,基于这篇文章:https://docs.databricks.com/user-guide/faq/send-email.html

我正在按照步骤进行操作,但出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte 而且,我认为,原因是因为在函数 makeCompatibleImage 内部我们有这个片段:val = "" % base64.standard_b64encode(png.read()),并且base64.standard_b64encode可能有问题

import numpy as np
import matplotlib.pyplot as plt

# Compute pie slices
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)

ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)

# Use custom colors and opacity
for r, bar in zip(radii, bars):
    bar.set_facecolor(plt.cm.viridis(r / 10.))
    bar.set_alpha(0.5)
# Convert image add append to html array
html.append(makeCompatibleImage(ax))

#
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<command-890455078841631> in <module>()
     16     bar.set_alpha(0.5)
     17 # Convert image add append to html array
---> 18 html.append(makeCompatibleImage(ax))

<command-890455078841625> in makeCompatibleImage(image, withLabel)
     11   val = None
     12   with open(imageName) as png:
---> 13     val = "<img src='data:image/png;base64,%s'>" % base64.standard_b64encode(png.read())
     14 
     15   displayHTML(val)

/databricks/python/lib/python3.6/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

我想知道如何复制这篇文章。

【问题讨论】:

【参考方案1】:

makeCompatibleImage 函数中添加以下代码,以二进制模式读取文件,对我有用:

 with open(imageName, 'rb') as png:

【讨论】:

以上是关于从 Databricks 笔记本发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

发送“代表”电子邮件

从 databricks 表的字符串列中收集用户信息(电子邮件、IP 地址等)

Access 2016 定时发送自动电子邮件

通过 php 脚本定期发送电子邮件

电子邮件传输的协议是啥?

Mime 和 Office365