如何从 Databricks 发送电子邮件
How to Send Emails From Databricks
我已使用 中的代码尝试从我的 Databricks 社区版发送代码:
我使用了以下代码:
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
from_email = "myname@myemail.co.uk"
to_email = "myname@myemail.co.uk"
def send_mail(send_from = from_email, send_to = to_email, subject = "Test", message = "Test", files=["/FileStore/tables2/"],
server="smtp.hosts.co.uk", port=587, username='myusername.co.uk', password='!L3qGWGuyw',
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',
'attachment; filename="{}"'.format(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()
如您所见,代码几乎相同。但是,当我 运行 代码时,出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/FileStore/tables2/'
这个错误是否也是因为我运行正在使用 Databricks Community Edition,就像上一个问题中的情况一样。
/FileStore/tables2/
只是您要作为附件发送的文件的名称。您需要将文件名放在那里,或者如果您不想发送附件,则将列表清空。它应该是一个本地文件,因此在 Azure 上使用 /dbfs/....
,在社区版上使用 .
我已使用
我使用了以下代码:
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
from_email = "myname@myemail.co.uk"
to_email = "myname@myemail.co.uk"
def send_mail(send_from = from_email, send_to = to_email, subject = "Test", message = "Test", files=["/FileStore/tables2/"],
server="smtp.hosts.co.uk", port=587, username='myusername.co.uk', password='!L3qGWGuyw',
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',
'attachment; filename="{}"'.format(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()
如您所见,代码几乎相同。但是,当我 运行 代码时,出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: '/FileStore/tables2/'
这个错误是否也是因为我运行正在使用 Databricks Community Edition,就像上一个问题中的情况一样。
/FileStore/tables2/
只是您要作为附件发送的文件的名称。您需要将文件名放在那里,或者如果您不想发送附件,则将列表清空。它应该是一个本地文件,因此在 Azure 上使用 /dbfs/....
,在社区版上使用