Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python执行shell邮件命令_Python_Email_Process_Subprocess_Popen3 - Fatal编程技术网

使用python执行shell邮件命令

使用python执行shell邮件命令,python,email,process,subprocess,popen3,Python,Email,Process,Subprocess,Popen3,我已经使用下面的代码发送了一封电子邮件,正如一篇关于类似主题的帖子所建议的那样。但邮件尚未发送。有什么建议吗 import subprocess recipient = 'xxxxx@gmail.com' subject = 'test' body = 'testing mail through python' def send_message(recipient, subject, body): process = subprocess.Popen(['mail', '-s', sub

我已经使用下面的代码发送了一封电子邮件,正如一篇关于类似主题的帖子所建议的那样。但邮件尚未发送。有什么建议吗

import subprocess
recipient = 'xxxxx@gmail.com'
subject = 'test'
body = 'testing mail through python'
def send_message(recipient, subject, body):
    process = subprocess.Popen(['mail', '-s', subject, recipient],
                               stdin=subprocess.PIPE)
    process.communicate(body)

print("sent the email")

您的函数可能无法调用,请尝试以下代码:

import subprocess

recipient = 'xxxxx@gmail.com'
subject = 'test'
body = 'testing mail through python'

def send_message(recipient, subject, body):
    try:
      process = subprocess.Popen(['mail', '-s', subject, recipient],
                               stdin=subprocess.PIPE)
    except Exception, error:
      print error
    process.communicate(body)

send_message(recipient, subject, body)

print("sent the email")

梅在工作。祝您好运。

您是否调用了函数send_message()?
mail-s…
是否从您机器上的命令行工作?若否,<代码>子流程无法使其工作。如果在windows
Traceback(最近一次调用)中运行,您可能会看到以下错误:文件“C:/Python34/Scripts/sending_gmail_3.py”,第12行,在发送消息(收件人、主题、正文)文件“C:/Python34/Scripts/sending_gmail_3.py”,第10行,在发送消息stdin=subprocess.PIPE)文件“C:\Python34\lib\subprocess.py”中,第858行,在初始化还原信号中,启动新会话)文件“C:\Python34\lib\subprocess.py”,第1111行,在执行子startupinfo)文件NotFoundError:[WinError 2]中,系统当然找不到指定的文件。Windows中不存在
mail
命令。在Python3.x中,更改为
body=b“通过Python测试邮件”
以工作