我目前正在使用此代码通过Python生成电子邮件:
from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def Create_Email():
msg = MIMEMultipart('alternative')
msg['Subject'] = 'My Subject'
msg['To'] = '[email protected]'
html = """\
<html>
<head></head>
<body>hello world</body>
</html>"""
part = MIMEText(html, 'html')
msg.attach(part)
outfile_name = r'C:\Downloads\email_sample.eml'
with open(outfile_name, 'w') as outfile:
gen = generator.Generator(outfile)
gen.flatten(msg)
Create_Email()
但是当我使用Outlook打开文件时,它显示为已经发送电子邮件:
如何更改此设置,以便将保存的文件视为草稿,我仍然可以对其进行编辑然后发送?像这样:
如果email.generator
无法做到这一点,我很乐意使用替代软件包。
与您使用的模块不同。
import win32com.client as win32
def create_mail(text, subject, recipient, send=True):
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
if send:
mail.send()
else:
mail.save()
create_mail("Hello World!", "Test-Mail", "[email protected]", send=False)
我发现的解决方案最终很简单;只需添加此标题:
msg.add_header('X-Unsent', '1')
它将电子邮件标记为未发送,即草稿。