我正在使用名为 Alteryx 的工作流程自动化程序。它有自己的“发送电子邮件”工具,但本质上仅限于 10mb 附件,而且我有许多每日/每周报告都比这个大。我正在尝试设计一个简短的示例工作流程,它可以接收代表电子邮件不同部分的一组变量。除了电子邮件正文之外,我的每一部分都在工作。我试图以某种方式编写它,以便任何内容都可以作为正文字符串变量输入,但似乎单引号、双引号和斜杠破坏了该变量,尽管电子邮件仍然仅以空正文发送。
有没有办法可以对整个变量进行编码,让 python 忽略输入字符串中的每个转义字符?我应该在用 ''' 等替换 ' 的 Python 步骤之前构建一些处理吗?
# List all non-standard packages to be imported by your
# script here (only missing packages will be installed)
from ayx import Package
#Package.installPackages(['pandas','numpy'])
from ayx import Alteryx
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
pyArray = Alteryx.read("#1")
body = pyArray["pymailBody"][0]
msg = MIMEMultipart()
msg['From'] = pyArray["pymailFrom"][0]
msg['To'] = pyArray["pymailTo"][0]
msg['CC'] = pyArray["pymailCC"][0]
msg['BCC'] = pyArray["pymailBCC"][0]
msg['Subject'] = pyArray["pymailSubject"][0]
msg['Body'] = body.encode('unicode_escape')
attachmentName = pyArray["pymailAttachmentName"][0]
attachmentPath = pyArray["pymailAttachmentPath"][0]
part = MIMEBase('application', "octet-stream")
part.set_payload(open(attachmentPath, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=attachmentName)
msg.attach(part)
with smtplib.SMTP("mail.company.com", 25) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.send_message(msg)
您的代码似乎是为 Python 3.5 或更早版本编写的。
email
库在 3.6 中进行了彻底修改,现在更加通用和合乎逻辑。我怀疑只需升级到该库的现代版本就可以解决您的难题。
在字里行间阅读的内容超出了我的喜好,我猜你的代码应该重构为类似的内容
from email.message import EmailMessage
import smtplib
from ayx import Package
from ayx import Alteryx
pyArray = Alteryx.read("#1")
body = pyArray["pymailBody"][0]
msg = EmailMessage()
msg['From'] = pyArray["pymailFrom"][0]
msg['To'] = pyArray["pymailTo"][0]
msg['CC'] = pyArray["pymailCC"][0]
msg['BCC'] = pyArray["pymailBCC"][0]
msg['Subject'] = pyArray["pymailSubject"][0]
msg['Body'] = body # no encoding here, just pass in the Unicode text
attachmentName = pyArray["pymailAttachmentName"][0]
attachmentPath = pyArray["pymailAttachmentPath"][0]
with open(attachmentPath, "rb") as payload:
msg.add_attachment(
payload.read(),
maintype='application', subtype='octet-stream',
filename=attachmentName)
with smtplib.SMTP("mail.company.com", 25) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.send_message(msg)