如何使用 Python 生成和打开 Outlook 电子邮件(但不发送)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20956424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I generate and open an Outlook email with Python (but do not send)
提问by wnnmaw
I have a script that automatically creates and sends emails sends emails using the simple function below:
我有一个脚本,它使用下面的简单函数自动创建和发送电子邮件发送电子邮件:
def Emailer(text, subject, recipient):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
mail.send
But how do I open this email in an Outlook window so that it can be manually edited and sent?
但是如何在 Outlook 窗口中打开这封电子邮件,以便可以手动编辑和发送它?
Ideally, I'd like something like this:
理想情况下,我想要这样的东西:
def __Emailer(text, subject, recipient, auto=True):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
if auto:
mail.send
else:
mail.open # or whatever the correct code is
采纳答案by Dmitry Streblechenko
Call mail.Display(True)instead of mail.send
调用mail.Display(True)而不是mail.send
回答by Sudath Murari
tldr: Use mail.Display(False)instead of mail.Display(True)
tldr:使用mail.Display(False)代替mail.Display(True)
mail.Display(False) will still display the window. If you use mail.Display(True) the scripts stops until the window is closed. So use mail.Display(False) this will open the window and your python script will move on to the next command. It is also useful to know that you can use mail.save() to save as draft in the draft folder.
mail.Display(False) 仍将显示窗口。如果您使用 mail.Display(True) 脚本将停止,直到窗口关闭。所以使用 mail.Display(False) 这将打开窗口,您的 python 脚本将继续执行下一个命令。知道您可以使用 mail.save() 在草稿文件夹中另存为草稿也很有用。
Visit https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/mailitem-display-method-outlookto read more on this
访问https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/mailitem-display-method-outlook阅读更多相关信息
回答by Max
Here is another option with saving the mail on disk first:
这是首先将邮件保存在磁盘上的另一种选择:
import webbrowser
mail.SaveAs(Path=save_path)
webbrowser.open(save_path)
This way the mail opens maximized.
这样邮件打开最大化。
回答by animati
I like the solution :) But I want to add some infos:
我喜欢这个解决方案 :) 但我想添加一些信息:
Using the solution, it is probably the best way to add a mail input with Html format for modification.
使用该解决方案,添加Html格式的邮件输入进行修改可能是最好的方法。
Also add the file from the working directory...
还要从工作目录中添加文件...
#requirements.txt add for py 3 -> pypiwin32
def Emailer(text, subject, recipient):
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipient
mail.Subject = subject
mail.HtmlBody = text
###
attachment1 = os.getcwd() +"\file.ini"
mail.Attachments.Add(attachment1)
###
mail.Display(True)
MailSubject= "Auto test mail"
MailInput="""
#html code here
"""
MailAdress="[email protected];[email protected]"
Emailer(MailInput, MailSubject, MailAdress ) #that open a new outlook mail even outlook closed.

