python 如何通过 mailx 和 subprcoess 发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2020476/
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 send a mail via mailx & subprcoess?
提问by Lior Dagan
I am EE, trying to write a script to simplify file checks using Python. For some reason, our IT will not let me gain access to our smtp server, and will only allow sending mail via mailx. So, I've thought of running mailx from Python and send it, in the same way that it works in my console. Alas, it gives an exeption. See Linux log below:
我是 EE,正在尝试编写一个脚本来简化使用 Python 的文件检查。出于某种原因,我们的 IT 不允许我访问我们的 smtp 服务器,并且只允许通过 mailx 发送邮件。所以,我想过从 Python 运行 mailx 并发送它,就像它在我的控制台中的工作方式一样。唉,它给出了一个例外。请参阅下面的 Linux 日志:
***/depot/Python-3.1.1/bin/python3.1
Python 3.1.1 (r311:74480, Dec 8 2009, 22:48:08)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> process=subprocess.Popen('echo "This is a test\nHave a loook see\n" | mailx -s "Test Python" [email protected]')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/depot/Python-3.1.1/lib/python3.1/subprocess.py", line 646, in __init__
errread, errwrite)
File "/depot/Python-3.1.1/lib/python3.1/subprocess.py", line 1146, in _execute_child
raise child_exception***
I am a newbe to Python (now migrating from PERL). Any thoughts?
我是 Python 的新手(现在从 PERL 迁移)。有什么想法吗?
回答by ghostdog74
you can use smtplib
你可以使用 smtplib
import smtplib
# email options
SERVER = "localhost"
FROM = "[email protected]"
TO = ["root"]
SUBJECT = "Alert!"
TEXT = "This message was sent with Python's smtplib."
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER)
server.set_debuglevel(3)
server.sendmail(FROM, TO, message)
server.quit()
If you really want to use subprocess( which i advise against)
如果你真的想使用子流程(我不建议这样做)
import subprocess
import sys
cmd="""echo "test" | mailx -s 'test!' root"""
p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = p.communicate()
print errors,output
回答by F0RR
You cas use subprocess.call
. Like:
你 cas 使用subprocess.call
. 喜欢:
subprocess.call(["mailx", "-s", "\"Test Python\"", "[email protected]"])
Details here
详情在这里
回答by RegularlyScheduledProgramming
Lior Dagan's code was close to being correct/functional: the error in this approach is a missing shell=True
kwarg
in the call to subprocess.Popen
. Anyone actually considering this approach should be aware that the subprocess
documentation warns that:
Lior Dagan 的代码几乎是正确的/功能性的:这种方法中的错误是shell=True
kwarg
在调用subprocess.Popen
. 任何真正考虑这种方法的人都应该知道,subprocess
文档警告说:
Invoking the system shell with shell=True can be a security hazard if combined with untrusted input.
Generally F0RR's and ghostdog74's solutions should be preferred as they are more robust and secure.
通常应该首选 F0RR 和 ghostdog74 的解决方案,因为它们更健壮和安全。