python - 如何在python的电子邮件正文中打印一个多行字符串变量
问题描述
我有这段代码:
l = ["Jargon", "Hello", "This", "Is", "Great"]
result = "\n".join(l[1:])
print result
输出:
Hello
This
Is
Great
我正在尝试将其打印到电子邮件正文中,如下所示,我将文本作为附件而不是正文。谁能告诉我我是否在这里遗漏了什么?
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
fp = open(file.csv, 'r')
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", fileame='file.csv')
msg.attach(attachment)
msg.attach(MIMEText(result, "plain"))
server = smtplib.SMTP("localhost")
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
解决方案
使用时yagmail
,它按预期工作。
import yagmail
yag = yagmail.SMTP(
user=conf_yag['user'],
password=conf_yag['password'])
l = ["Jargon", "Hello", "This", "Is", "Great"]
result = "\n".join(l[1:])
yag.send(emailto, 'test from yagmail', result)
# including attachment
yag.send(emailto,
subject='test from yagmail',
contents=result,
attachments='somefile.txt')
其中conf_yag
存储您的凭据,emailto
是收件人电子邮件地址,'somefile.txt'
是文件附件。
推荐阅读
- symfony - 在奏鸣曲管理员列表中使用自定义列
- ios - 为什么即使条件为假,segue 也会执行?斯威夫特,iOS
- python - 如何找到每个客户的相似地址数量?
- linux - 修剪文本表并将值存储为变量
- java - 保存 Spark Java RDD,以便将每个 RDD 值保存到单独文件夹中的单独文件中
- java - Java程序计算变量数组的平均值,从命令行输入,显示结果
- verilog - 为什么verilog教程通常使reset异步?
- java - Java keyRelease 似乎不起作用
- node.js - Nodejs Exec 函数不适用于多行 Shell 脚本
- java - Google Sheet API:仅在第一张纸上打印 - Java