首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mailx不会通过python发送

Mailx不会通过python发送
EN

Stack Overflow用户
提问于 2021-01-28 09:51:14
回答 1查看 112关注 0票数 0

我正在为python重写一个shell脚本,其中一部分包括通过mailx发送通知。我似乎不能正确处理子进程。

result = subprocess.run(["/bin/mailx", "-r", "sender@email.com", "-s", "Test", "recipient@email.com"], check=True)

当我在服务器上运行这个命令时,命令返回一个空行,“将不完成”,我认为这可能是因为mailx在等待电子邮件正文,因为当我尝试在没有正文的情况下发送bash时,我得到了类似的问题,所以我得到了以下提示:

  1. result = subprocess.run(["echo", "Testing", "|", /bin/mailx", "-r", "sender@email.com", "-s", "Test", "recipient@email.com"], check=True)
  2. result = subprocess.run(["/bin/mailx", "-r", "sender@email.com", "-s", "Test", "recipient@email.com", b"Testingtesting"], check=True)

在测试1时,它只会在echo之后回显所有内容。当测试2时,我再次得到空白行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-28 11:53:14

使用subprocess.Popen,您可以如下所示:

代码语言:javascript
复制
import subprocess
cmd = """
        echo 'Message Body' | mailx -s 'Message Title' -r sender@someone.com receiver@example.com 
      """

result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output, errors = result.communicate()

关于来自shell=True文档

shell=False禁用所有基于shell的特性,但不受此漏洞的影响;请参阅Popen构造函数文档中的备注,以获得使shell=False正常工作的有用提示。在命令字符串是从外部输入构造的情况下,强烈禁止使用shell=True。

在您的情况下,如果您没有将用户输入传递给subprocess.Popen,那么您是安全的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65934654

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档