使用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…
是否从您机器上的命令行工作?若否,<代码>子流程无法使其工作。如果在windowsTraceback(最近一次调用)中运行,您可能会看到以下错误:文件“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测试邮件”
以工作